LateInitializationError: Field 'currentLatLng' has not been initialized in Flutter

LateInitializationError: Field 'currentLatLng' has not been initialized in Flutter

我正在使用此库在 google 地图上显示我的当前位置:package:location/location.dartPub Dev Link

问题是每当我在 发布模式 下打开应用程序时,它就会崩溃并显示:

LateInitializationError: Field 'currentLatLng' has not been initialized

它不会在 Android 设备上的 调试模式 中崩溃。 但是,它确实会在 iOS (发布和调试模式).

上崩溃

我不确定我做错了什么。这是我的小部件和尝试:

    class TestGoogle extends StatefulWidget {
      const TestGoogle({Key? key}) : super(key: key);
      @override
      _TestGoogleState createState() => _TestGoogleState();
    }

    class _TestGoogleState extends State<TestGoogle> {
      late LatLng currentLatLng;
      late GoogleMapController mapController;
      Location location = new Location();
      var latitude;
      var longitude;
      late LocationData _locationData;
      get() async {
        _locationData = await location.getLocation();
        latitude = _locationData.latitude;
        longitude = _locationData.longitude;
        setState(() {
          currentLatLng = new LatLng(latitude, longitude);
        });
      }
      @override
      initState() {
        super.initState();
        get();
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GoogleMap(
            myLocationEnabled: true,
            onCameraMove: (CameraPosition cameraPosition) {
              print(cameraPosition.zoom);
            },
            initialCameraPosition:
                CameraPosition(target: currentLatLng, zoom: 15.0),
          ),
        );
      }
    }

我通过

解决了这个问题
class TestGoogle extends StatefulWidget {
  const TestGoogle({Key? key}) : super(key: key);
  @override
  _TestGoogleState createState() => _TestGoogleState();
}

class _TestGoogleState extends State<TestGoogle> {
  LatLng? currentLatLng;
  late GoogleMapController mapController;
  Location location = new Location();
  var latitude;
  var longitude;
  late LocationData _locationData;
  get() async {
    _locationData = await location.getLocation();
    latitude = _locationData.latitude;
    longitude = _locationData.longitude;
    setState(() {
      currentLatLng = new LatLng(latitude, longitude);
    });
  }
  @override
  initState() {
    super.initState();
    get();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: currentLatLng == null
          ? Center(child: PlatformCircularProgressIndicator())
          : GoogleMap(
        myLocationEnabled: true,
        onCameraMove: (CameraPosition cameraPosition) {
          print(cameraPosition.zoom);
        },
        initialCameraPosition:
            CameraPosition(target: currentLatLng, zoom: 15.0),
      ),
    );
  }
}

以下代码应该可以解决您的问题。您的问题原因,请在初始化前尝试使用变量。


class TestGoogle extends StatelessWidget {
  TestGoogle({Key? key}) : super(key: key);

  late GoogleMapController mapController;
  Location location = Location();

  Future<LatLng> get() async {
    final _locationData = await location.getLocation();
    return LatLng(_locationData.latitude, _locationData.longitude);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<LatLng>(
        future: get(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final locationModel = snapshot.data!;
            final latitude = locationModel.latitude;
            final longitude = locationModel.longitude;

            return GoogleMap(
              myLocationEnabled: true,
              onCameraMove: (CameraPosition cameraPosition) {
                print(cameraPosition.zoom);
              },
              initialCameraPosition: CameraPosition(target: locationModel, zoom: 15.0),
            );
          }
          return const CircularProgressIndicator();
        },
      ),
    );
  }
}