Flutter 如何在初始化前短时间内处理延迟变量?

Flutter 如何在初始化前短时间内处理延迟变量?,flutter,Flutter,在我的应用程序中,我使用geolocator包获取用户的当前位置。除了在地图用户界面加载前一秒钟之外,一切正常。问题是, late Position currentLocation; 此变量将在一秒钟内设置。在设置数据之前,dart会在很短的时间内抛出此错误: LateInitializationError:字段“currentLocation”尚未初始化。 之后,一切都恢复正常。我尝试不使用“late”,但后来出现了以下错误: 必须初始化不可为空的实例字段“currentLocation”。

在我的应用程序中,我使用geolocator包获取用户的当前位置。除了在地图用户界面加载前一秒钟之外,一切正常。问题是,

late Position currentLocation;
此变量将在一秒钟内设置。在设置数据之前,dart会在很短的时间内抛出此错误:

LateInitializationError:字段“currentLocation”尚未初始化。

之后,一切都恢复正常。我尝试不使用“late”,但后来出现了以下错误:

必须初始化不可为空的实例字段“currentLocation”。尝试添加一个初始化表达式,或一个初始化它的生成式构造函数,或将其标记为“late”

我试着在我的地图界面中使用一个CircularProgressIndicator,直到它得到值,但无论如何它都不会变为null。。。在这种情况下我该怎么办? 这是我的全班同学,如果需要的话:

class ApplicationBloc with ChangeNotifier {
  final geoLocatorService = GeoLocatorService();
  final placesService = PlacesService();

  late Position currentLocation;
  List<PlaceSearch> searchResults = [];

  ApplicationBloc() {
    setCurrentLocation();
  }
  
  setCurrentLocation() async {
    currentLocation = await geoLocatorService.getCurrentLocation();
    notifyListeners();
  }

  searchPlaces(String searchTerm, double radius) async {
    searchResults = await placesService.getAutoComplete(searchTerm, radius);
    notifyListeners();
  }
}
class ApplicationBloc与ChangeNotifier{
最终地理定位服务=地理定位服务();
最终地点服务=地点服务();
后期定位;
列出搜索结果=[];
ApplicationBloc(){
setCurrentLocation();
}
setCurrentLocation()异步{
currentLocation=等待地理定位服务。getCurrentLocation();
notifyListeners();
}
searchPlaces(字符串searchTerm,双半径)异步{
searchResults=await placesService.getAutoComplete(搜索术语,半径);
notifyListeners();
}
}

你能不能把时间推迟到
并分配一些初始实例,比如
currentLocation=Position(0,0)然后用一个实际值替换它?当我这样做时,映射以该初始值开始。我还以为它以后会被替换,但没有用新的值替换。如果地图加载默认位置是不可接受的,那么我们必须将地图放入
FutureBuilder
中,并在数据可用且地图可以显示时重建。关于
currentLocation
未替换为新值,这与
late
或not
late
无关。任何非
final
的变量都可以更新。如果它不更新,那就是一个编码错误。好的,我将使用FutureBuilder重建我的代码。非常感谢!欢迎祝你好运