PlatformException(PlatformException(渲染器已初始化,渲染器初始化调用多次,null,null))

问题描述 投票:0回答:1

我尝试使用 Google Maps Flutter 的最新地图渲染器,使用此包

google_maps_flutter_android: ^2.6.0

这是我的主要功能:

main() async {
  ...
  
  AndroidMapRenderer mapRenderer = AndroidMapRenderer.platformDefault;

  final GoogleMapsFlutterPlatform mapsImplementation =
    GoogleMapsFlutterPlatform.instance;
  if (mapsImplementation is GoogleMapsFlutterAndroid) {
    WidgetsFlutterBinding.ensureInitialized();
    mapRenderer = await mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest);

  ...
  runApp(MyApp);
}

它有效,但自从他们提到以来

必须在创建 GoogleMap 实例之前请求渲染器,因为每个应用程序上下文只能初始化渲染器一次。

因此,每当我尝试热重启时,它都会返回

PlatformException (PlatformException(Renderer already initialized, Renderer initialization called multiple times, null, null))

当我需要热重启时,我需要注释那部分代码。

然后我尝试对这部分代码使用 try catch,但它仍然返回异常并暂停应用程序。

代码看起来像这样:

main() async {
  ...
  
  AndroidMapRenderer mapRenderer = AndroidMapRenderer.platformDefault;

  try {
    final GoogleMapsFlutterPlatform mapsImplementation =
      GoogleMapsFlutterPlatform.instance;
    if (mapsImplementation is GoogleMapsFlutterAndroid) {
      WidgetsFlutterBinding.ensureInitialized();
       mapRenderer = await mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest);
    }
  } catch (e) {
    log(e.toString());
  }

  ...
  runApp(MyApp);
}

有没有办法只运行该部分代码一次,即使热重载时它也不会重新运行该部分代码?

flutter dart google-maps
1个回答
0
投票

我也有同样的问题。但由于这个错误,我想使用旧版渲染器https://github.com/flutter/flutter/issues/122401

因此,要在热重启时“忽略”此错误,您可以不要在

await
 上调用 
mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest);

它不会在调试模式下崩溃或停止您的应用程序,并且可以在发布模式下工作(因为它没有热重启)。

代码:

    final GoogleMapsFlutterPlatform mapsImplementation = GoogleMapsFlutterPlatform.instance;
    if (mapsImplementation is GoogleMapsFlutterAndroid) {
       WidgetsFlutterBinding.ensureInitialized();
       mapsImplementation.initializeWithRenderer(AndroidMapRenderer.latest);
    }

    WidgetsFlutterBinding.ensureInitialized();

我认为问题仍然存在,因为 Google 地图插件本机初始化渲染器,但热重启实际上并没有重新启动 Android 中的应用程序,并且渲染器保持初始化状态。

© www.soinside.com 2019 - 2024. All rights reserved.