Flutter 飞舞的未来总是归零
我的代码中有什么错误?它总是返回nullFlutter 飞舞的未来总是归零,flutter,flutter-dependencies,Flutter,Flutter Dependencies,我的代码中有什么错误?它总是返回null getLocation().then((r) { if (r != null) { print("r=" + r.length.toString()); } else { print("result is 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;
});
}
getLocation()。然后((r){
如果(r!=null){
打印(“r=“+r.length.toString());
}否则{
打印(“结果为空”);
}
});
FuturegetLocation()异步{
//打印(“getLocation调用”);
位置=新位置();
列表结果=[];
location.getLocation()然后((loc){
结果。添加(位置纬度);
结果。添加(位置经度);
结果.增加(4213);
//打印(loc.latitude.toString()+”,“+loc.longitude.toString()+”l=“+l1.length.toString());
返回结果;
}).catchError((e){
返回结果;
});
}
看起来你的帖子大部分都是代码;请添加更多详细信息。
看起来你的帖子大部分都是代码;请添加更多详细信息。
看起来你的帖子大部分都是代码;请添加更多详细信息。
看起来你的帖子大部分都是代码;请添加更多详细信息。
您没有在函数中返回任何内容,只在
然后回调中返回。
由于您使用的是异步语法,您可以选择:
Future< List<double>> getLocation() async {
location = new Location();
List<double> result=[];
var loc = await location.getLocation();
result.add(loc.latitude);
result.add(loc.longitude);
result.add(4213);
return result;
}
FuturegetLocation()异步{
位置=新位置();
列表结果=[];
var loc=await location.getLocation();
结果。添加(位置纬度);
结果。添加(位置经度);
结果.增加(4213);
返回结果;
}
我已将错误处理从代码中删除,但如果需要,您可以使用try-catch。请添加更多详细信息。