Flutter Google Maps,尝试创建已创建的平台视图,视图ID:0


17

第一次,谷歌地图抖动,加载完美,但当热重启时,它进入平台异常

google_maps_flutter:^ 0.5.21 + 15

Github [google_maps_flutter]试图创建一个已经创建的平台视图#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

扑医生-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 -0800Engine 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/SdkAndroid NDK location not configured (optional; useful for native profiling support)Platform android-29, build-tools 29.0.2Java 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.1Dart plugin version 191.8593Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[] Connected device (1 available)
    • vivo 172349269ad3 • android-arm64 • Android 9 (API 28)No issues found!

Answers:


7

为了解决这个问题,我通过终端进行了以下操作:

1-将分支更改为主分支:

flutter channel master

2-升级的颤振

flutter upgrade

3-清除代码:

flutter clean


1
是的,我已经做过相同的工作,现在热重装正在工作,更改为master分支,升级了flutter和flutter clean,然后应用程序正在工作
Lav Kushwaha

1
切换到主通道并升级颤振将解决此问题。多谢您+1。
Vinoth Vino

1
切换到master分支是一个坏主意。主分支最不稳定。谁在寻找更好的解决方案,应该考虑这个问题的另一个答案。 github.com/flutter/flutter/wiki/Flutter-build-release-channels
照片

1
flutter clean如果有人可以编辑,它应该是小写字母
Dimitar

1
@Dimitar我没有注意到它是大写字母,我刚刚对其进行了编辑,谢谢您通知我们,
Yousef Gamal

7

我也有同样的问题,使用flutter clean并没有为我解决(flutter版本1.12.13 + hotfix 8

但是,然后向窗口小部件添加唯一键(而不是其状态)对我来说解决了这个问题。

最低工作代码示例来证明这一点。

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,
          ),
        )
    );
  }
}

感谢Eyad,添加密钥有助于解决问题
Charden Daxicen

在Flutter 1.17稳定版发布之前,我认为这是生产项目的正确答案
Pablo Insua

您能详细说明一下为什么不使用in键GoogleMap()吗?
Shahzad Akram,

4

根据API

在对地图进行布局之前,请勿使用此相机更新来更改相机(为了使此方法正确确定适当的边界框和缩放级别,地图必须具有尺寸)。否则将抛出IllegalStateException。

我遇到了同样的问题,经过一些调试后,看起来无论何时在查看地图时进行热重载,都会出现错误。

但是,如果在放置地图后进行热重装,则可以防止该错误。

换句话说,可以防止多个地图实例处于活动状态。

因此,我建议在开发过程中在热加载之前放置地图。

在生产版本中,用户将无法热重载,因此他们将不会遇到平台问题。



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.