19

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

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 -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!
4

5 回答 5

9

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

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

flutter channel master

2-升级的颤振

flutter upgrade

3-清理代码:

flutter clean

于 2020-02-07T18:54:20.407 回答
9

我也有同样的问题,使用flutter clean并没有为我解决(颤振版本 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,
          ),
        )
    );
  }
}
于 2020-02-18T15:43:34.543 回答
5

问题是由于在多个 GoogleMaps 小部件中使用了相同的控制器。只构建一个 GoogleMap 小部件并使用 GoogleMapController 对象进行任何更改。

于 2020-01-06T07:41:50.223 回答
4

根据API

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

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

但是,如果您在处理地图后热重新加载,则可以防止错误。

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

所以我建议在开发过程中热重载之前处理地图。

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

于 2020-01-06T00:21:27.000 回答
2

此处引用了该问题:https ://github.com/flutter/flutter/issues/45695

于 2020-01-07T21:50:47.270 回答