首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振:非空实例字段“currentLocation”必须初始化。

颤振:非空实例字段“currentLocation”必须初始化。
EN

Stack Overflow用户
提问于 2021-07-27 13:48:25
回答 1查看 169关注 0票数 0

我得到了一个错误,与新的颤振变化的零安全。我不确定我是否该加最后/迟/!在这个密码里。

我有一个代码连接到Google,现在我希望能够存储收集到的数据。

现在我在Application:Non-nullable field 'currentLocation' must be initialized.上遇到了一个错误

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:hawkepedia/services/geolocator_Services.dart';

import 'package:geolocator/geolocator.dart';

class ApplicationBloc with ChangeNotifier {
  final geoLocatorService = GeolocatorService();

  //Variables
 Position currentLocation;

  //fire function when the app starts
  ApplicationBloc(){
    setCurrentLocation();
  }

  //gets current location
  setCurrentLocation() async {
    currentLocation = await geoLocatorService.getCurrentLocation();
    notifyListeners();
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-27 13:57:20

由于它是错误中的状态,导致问题的字段是currentLocation。

您可以:

例如,

  • 将其设置为late,这意味着您将在initState期间初始化它。
  • 将其设置为可空,在本例中不确定它是否可行,Position? currentLocation。(但对于主types)
  • Initialize而言,它具有一个您知道尚未初始化的中性值

)。

编辑:

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:hawkepedia/services/geolocator_Services.dart';

import 'package:geolocator/geolocator.dart';

class ApplicationBloc with ChangeNotifier {
  final geoLocatorService = GeolocatorService();

  //Variables
 late Position currentLocation;

  //fire function when the app starts
  ApplicationBloc(){
    setCurrentLocation();
  }

  //gets current location
  setCurrentLocation() async {
    currentLocation = await geoLocatorService.getCurrentLocation();
    notifyListeners();
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68546125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档