Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!
like image 526
Lavkant Kachhwaha Avatar asked Dec 28 '19 06:12

Lavkant Kachhwaha


People also ask

What is Google Maps flutter?

1. Introduction Flutter is Google's mobile app SDK for crafting high-quality native experiences on iOS and Android in record time. With the Google Maps Flutter plugin, you can add maps based on Google maps data to your application.

What are platform views in flutter?

Platform views allow you to embed native views in a Flutter app, so you can apply transforms, clips, and opacity to the native view from Dart. This allows you, for example, to use the native Google Maps from the Android SDK directly inside your Flutter app. Note: This page discusses how to host your own native views within a Flutter app.

What is flutter?

Congratulations! What is Flutter? Congratulations! 1. Introduction Flutter is Google's mobile app SDK for crafting high-quality native experiences on iOS and Android in record time. With the Google Maps Flutter plugin, you can add maps based on Google maps data to your application.

How does the Google Maps plugin work?

The plugin automatically handles access to the Google Maps servers, map display, and response to user gestures such as clicks and drags. You can also add markers to your map.


2 Answers

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

like image 198
Yousef Gamal Avatar answered Sep 28 '22 11:09

Yousef Gamal


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,
          ),
        )
    );
  }
}
like image 20
Eyad Avatar answered Sep 28 '22 10:09

Eyad