Flutter Google Maps, Trying to create an already created platform view, view id: 0

前端 未结 5 2332
萌比男神i
萌比男神i 2021-02-18 22:15

For first time google maps flutter ,loads perfect but when hot restart it , it goes to platform exception

google_maps_flutter: ^0.5.21+15

Github [google_maps_fl

5条回答
  •  日久生厌
    2021-02-18 22:58

    I also had the same issue and using flutter clean didn't solve it for me (flutter version 1.12.13+hotfix 8)

    But then, adding a unique key to the widget (not to its state) solved the issue for me.

    minimum working code sample to demonstrate this.

    import 'package:flutter/material.dart';
    import 'package:google_maps_flutter/google_maps_flutter.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Map not crashing demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: LocationScreen(),
        );
      }
    }
    
    class LocationScreen extends StatefulWidget 
    {
      final Key _mapKey = UniqueKey();
      @override
      _LocationScreenState createState() => _LocationScreenState();
    }
    
    class _LocationScreenState extends State 
    {
      @override
      Widget build(BuildContext context) 
      {
        return Scaffold(
          appBar: AppBar(title: const Text('Map not crashing demo')),
          body: TheMap(key:widget._mapKey)
        );
      }
    }
    
    class TheMap extends StatefulWidget 
    {
      ///key is required, otherwise map crashes on hot reload
      TheMap({ @required Key key})
      :
      super(key:key);
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State 
    {
      GoogleMapController _mapController ;
    
      void _onMapCreated(GoogleMapController controller) {
        _mapController = controller;
      }
      @override
      Widget build(BuildContext context) 
      {
        return Scaffold(
          //also this avoids it crashing/breaking when the keyboard is up
          resizeToAvoidBottomInset: false,
          body: GoogleMap(
              onMapCreated: _onMapCreated,
              initialCameraPosition: CameraPosition(
                target: const LatLng(30.0925973,31.3219982),
                zoom: 11.0,
              ),
            )
        );
      }
    }
    

提交回复
热议问题