【问题标题】:LateInitializationError: Field 'currentLatLng' has not been initialized in FlutterLateInitializationError:字段“currentLatLng”尚未在 Flutter 中初始化
【发布时间】:2021-11-03 08:21:09
【问题描述】:

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

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

LateInitializationError: 字段 'currentLatLng' 尚未 初始化

它不会在 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),
          ),
        );
      }
    }

【问题讨论】:

  • 我已经解决了你的问题、拼写语法等问题。你是否尝试过使用 flutter run -v --release 运行详细输出?请包括那个输出。

标签: flutter dart location


【解决方案1】:

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


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();
        },
      ),
    );
  }
}

【讨论】:

  • 我有一个问题,当我在 Android 中打开它时,它会打开非常快,但如果我在 iOS 物理设备中打开它,它会打开非常慢(显示循环进度指示器)。
  • 这是一个有趣的问题,也许如果你在电脑上用以太网电缆连接互联网,那么连接速度很快,而 ios 设备的 wi-fi 连接速度相对于其他设备慢
  • 那么我如何在 iOS 设备中解决这个问题?
  • 你试过iOS模拟器吗?
  • 在模拟器中,我必须将硬编码的纬度和纬度放在模拟器设置上,从而限制使用模拟器。是的,如果我将硬编码的纬度和经度都放在模拟器或代码中,那么它工作正常。
【解决方案2】:

我修复了这个问题

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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2021-11-17
  • 2021-12-01
  • 2022-07-12
  • 1970-01-01
  • 2022-07-27
  • 2021-10-27
  • 2021-12-29
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多