Flutter Future始终返回null

时间:2019-06-02 10:04:31

标签: flutter flutter-dependencies

我的代码有什么问题,它总是返回null

getLocation().then((r)  {
                          if (r != null) {
                            print("r=" + r.length.toString());
                          } else {
                            print("result is null");
                          } 
                        });



Future< List<double>> getLocation() async {
   // print("getLocation called");
      location = new Location();
    List<double> result=[];

      location.getLocation().then((loc) {

      result.add(loc.latitude);
      result.add(loc.longitude);
      result.add(4213);
      //  print(loc.latitude.toString() + "," + loc.longitude.toString() +"  l="+l1.length.toString());
      return result;

    }).catchError((e){
      return result;
    });
  }

您的帖子似乎主要是代码;请添加更多详细信息。
看起来您的帖子大部分是代码;请添加更多详细信息。
看起来您的帖子大部分是代码;请添加更多详细信息。
看起来您的帖子大部分是代码;请添加更多详细信息。

1 个答案:

答案 0 :(得分:2)

您不会在函数中返回任何内容,仅在您的self.D = list(self.D) 回调中返回任何内容。 由于无论如何您都使用异步语法,因此您可以进行以下操作:

then

我已经从代码中排除了错误处理,但是如果需要,您可以使用try-catch。