方法 'listen' 没有为 class 'Function' 定义?

The method 'listen' isn't defined for the class 'Function'?

大家好,我正在尝试构建一个 flutter 来显示纬度和经度,但我仍然收到错误代码:

 void initState(){
    super.initState();
    //Default variable set 0
    currentLocation['latitude'] = 0.0;
    currentLocation['longtitude'] = 0.0;

    initPlatformState();
    locationSubscription = location.onLocationChanged.listen((Map<String, double> result){
      setState(() {
        currentLocation = result;
      });
    });
  }

onLocationChanged 是一个函数,所以你需要调用它来取回流 onLocationChanged().listen 应该做你需要的。

下面的代码对我有用

    var location = new Location();
    location.onLocationChanged().listen((LocationData currentLocation) {
      print(currentLocation.latitude);
      print(currentLocation.longitude);
    });