19

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_flutter] Trying to create an already created platform view #45695

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create an already created platform view, view id: 0

Flutter doctor -v

[✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Linux, locale en_IN)
    • Flutter version 1.12.13+hotfix.5 at /home/asus/Documents/Flutter_SDK/flutter
    • Framework revision 27321ebbad (2 weeks ago), 2019-12-10 18:15:01 -0800
    • Engine revision 2994f7e1e6
    • Dart version 2.7.0


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /home/asus/Android/Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /snap/android-studio/81/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Android Studio (version 3.5)
    • Android Studio at /snap/android-studio/81/android-studio
    • Flutter plugin version 42.1.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] Connected device (1 available)
    • vivo 1723 • 49269ad3 • android-arm64 • Android 9 (API 28)

• No issues found!

5 Answers 5

9

To solve this issue I did the following via the terminal:

1- Changed the branch to Master branch:

flutter channel master

2- Upgraded Flutter

flutter upgrade

3- Cleaned the code:

flutter clean

7
  • 1
    yes, i have done the same and hot reload is now working, changed to master branch, upgrade the flutter and flutter clean , then app is working Commented Feb 10, 2020 at 5:13
  • 1
    Switching to master channel and upgrading the flutter will solve the problem. Thanks +1 for you. Commented Mar 18, 2020 at 6:54
  • 3
    It's a bad idea to switch to master branch. Master branch is most unstable among all. Whoever looking for better solution should consider another answers for this question. github.com/flutter/flutter/wiki/Flutter-build-release-channels
    – fotonmoton
    Commented Mar 18, 2020 at 21:48
  • 1
    it should be flutter clean with lower case if someone can edit it
    – Dimitar
    Commented Apr 29, 2020 at 11:57
  • 1
    @Dimitar I didn't notice that it was upper case, I've just edited it, thanks for letting us know, Commented Apr 30, 2020 at 3:20
9

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<LocationScreen> 
{
  @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<TheMap> 
{
  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,
          ),
        )
    );
  }
}
3
  • Thanks Eyad, Adding Keys helped to solve the problem Commented Apr 8, 2020 at 17:44
  • Until the release of Flutter 1.17 stable, I think this is the correct answer for a project in production Commented Apr 19, 2020 at 15:04
  • Can you elaborate that why don't we use a key in GoogleMap()? Commented May 1, 2020 at 7:57
5

The issue is due to using the same controller in multiple GoogleMaps widget. Build only one GoogleMap widget and use the GoogleMapController object to make any changes.

4

According to the API,

Do not change the camera with this camera update until the map has undergone layout (in order for this method to correctly determine the appropriate bounding box and zoom level, the map must have a size). Otherwise an IllegalStateException will be thrown.

I had the same issue and after some debugging, it looks like whenever you hotreload while the map is in view, it will give the error.

But you can prevent the error if you hot reload after disposing the map.

In other words, it is there to prevent multiple instances of maps to be active.

So I would suggest to dispose the map before hot reload while in development.

In a production version, users won't be able to hot reload thus, they won't encounter the platform issue.

1
  • I'm having it now in production though
    – NeoKree
    Commented Sep 21, 2022 at 4:54
2

The issue is referenced here: https://github.com/flutter/flutter/issues/45695

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.