我得到了一个错误,与新的颤振变化的零安全。我不确定我是否该加最后/迟/!在这个密码里。
我有一个代码连接到Google,现在我希望能够存储收集到的数据。
现在我在Application:Non-nullable field 'currentLocation' must be initialized.上遇到了一个错误
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();
}
}发布于 2021-07-27 13:57:20
由于它是错误中的状态,导致问题的字段是currentLocation。
您可以:
例如,
late,这意味着您将在initState期间初始化它。Position? currentLocation。(但对于主types))。
编辑:
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();
}
}https://stackoverflow.com/questions/68546125
复制相似问题