如何在Flutter中为Google Map设置主题?

时间:2020-01-14 14:59:45

标签: google-maps flutter dart themes

我有一个地图应用程序,想要在日出和日落时更改主题。为地图设置主题应该非常简单,但出现以下错误。

未处理的异常:NoSuchMethodError:方法'setMapStyle'被调用为null。

这是我的pubspec.yaml

def highlight_false(cell):
    return '' if cell else 'background-color: yellow'

result = compare(df0,df2)
result.style.applymap(highlight_false)

我已经导入了以下软件包。

assets:
  - assets/themes/map/day/
  - assets/themes/map/night/

我已将以下行添加到我的initState()

import 'package:flutter/services.dart' show rootBundle;

这是我的地图控制器。

rootBundle.loadString('assets/themes/map/night/night.json').then((string) {
      _mapStyle = string;
    });

这是我的地图代码的开始

Completer<GoogleMapController> _mapController = Completer();

void _onMapCreated(GoogleMapController controller) {
    PermissionHandler()
        .checkPermissionStatus(PermissionGroup
            .locationWhenInUse) //check permission returns a Future
        .then(_updateStatus); // handling in callback to prevent blocking UI

    _mapController.complete(
     controller); // manages camera function (position, animation, zoom).

    controller.setMapStyle(_mapStyle);
    print("MAPSTYLE -> $_mapStyle");
  }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

此代码对我有用。

controller.setMapStyle(await rootBundle.loadString('assets/styles/google_map.json'));

使用异步等待以防止出现竞争状况。