簡體   English   中英

如何在 Flutter 中主題化 Google 地圖?

[英]How can I theme Google Map in Flutter?

我有一個地圖應用程序,我想在日出和日落時更改主題。 地圖主題應該非常簡單,但我收到以下錯誤。

未處理的異常:NoSuchMethodError:在 null 上調用了方法“setMapStyle”。

這是我的 pubspec.yaml

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

我已經導入了以下包。

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

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

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

這是我的地圖代碼的開始

_userLocation == null
    ? Center(
        child: CircularProgressIndicator(
            backgroundColor: Theme.UniColour.primary[900],
        ))
        : GoogleMap(
            onMapCreated: _onMapCreated,
            initialCameraPosition:
                CameraPosition(
                    target: _userLocation,
                    zoom: _defaultZoom,
                    tilt: _tiltAngle),
                    ...
                    ...
                    ...

任何幫助將不勝感激。

這段代碼對我有用。

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

使用異步等待來防止競爭條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM