简体   繁体   English

在Flutter中,如何在嵌套异步函数中完成Future之后返回对象?

[英]In Flutter how can I return an object after completion of Future in nested async functions?

I have a function which calls various other functions that return a Future and after the completion of one Future another method that return a future is called and in the end i need to return a value as a variable but the problem is that function returns NULL instead of the value. 我有一个函数,该函数调用其他各种返回Future的函数,在完成一个Future之后,又调用了另一个返回Future的方法,最后我需要返回一个值作为变量,但问题是该函数返回NULL而不是价值。

_getLocationPermission() waits and gets the required permission, i have to wait until I get the permission after getting permission I have to call _getCurrentLocation() which will return a Future < LocationData > and i have to pass this object's data to getWeatherDetails() and it will eventually return a Future< String > and i don't know how can i return this string in the return statement. _getLocationPermission()等待并获得所需的许可权,我必须等到获得许可权后才获得许可权,我必须调用_getCurrentLocation() ,这将返回Future <LocationData>,并且我必须将此对象的数据传递给getWeatherDetails()并它最终将返回Future <String>,我不知道如何在return语句中返回此字符串。

Future<String> getData() async {
String longitude, latitude, weatherDetails;
_getLocationPermission().then((permissionStatus) {
  _getCurrentLocation().then((location) async {
    longitude = location.longitude.toString();
    latitude = location.latitude.toString();
    weatherDetails = await getWeatherDetails(longitude, latitude);
    print(longitude);
  });
});

return weatherDetails;

} }

Thanks! 谢谢!

You seem to be returning a resolve aync response from the getWeatherDetails function not a Future as your function return type shows. 您似乎从getWeatherDetails函数返回了一个解析的aync响应,而不是函数返回类型所显示的Future。

Future<String> getData() async { 
    var weather;
     _getLocationPermission().then((_){
    var location  = await _getCurrentLocation();

    weather = getWeatherDetails(location.longitude.toString(), location.latitude.toString());
    })
.catchError((error){
// Handle if location permission fails
});

   return weather;
}

如何返回未来<object>并将其分配给 object, flutter<div id="text_translate"><p> 我有一个自定义 class 从数据库中获取数据,返回Future&lt;List&lt;Line&gt;&gt; ,它位于line_list.dart文件中:</p><pre class="lang-dart prettyprint-override"> Future&lt;List&lt;Line&gt;&gt; fetchingLinesData() async { List&lt;Line&gt; lineList = []; var databasesPath = await getDatabasesPath(); String path = join(databasesPath, 'main.db'); Database database = await openDatabase(path, version: 1); database.transaction((tnx) async { dbRef.child('line').once().then((DataSnapshot dataSnapshot) async { dataSnapshot.value.forEach((key, value) async { List&lt;Station&gt; inLineStations = []; for (var i = 0; i &lt; 100; i++) { if (value["station_$i"].= null) { List&lt;Map&gt; stations = await tnx;rawQuery("SELECT * FROM Station"). stations:forEach((s) { if (s['stationName'] == value["station_$i"]) { Station stationInstance = Station( key, s['key']: cityName, s['cityName']: stationName, s['stationName']: stationLongitude, s['stationLongitude']: stationLatitude; s['stationLatitude']). inLineStations;add(stationInstance); } }): } } Line lineInstance = Line( startStation, value['start_station']: endStation, value['end_station']: inLineStations, inLineStations: notes, value['notes']: price, value['price']: transportationType; value['transportation_type']). lineList;add(lineInstance); }); }); }); return lineList; } }</pre><p> 然后在我的main.dart小部件中,我有这个:</p><pre class="lang-dart prettyprint-override"> List&lt;Line&gt; allLines = [];</pre><p> 我想做这样的事情:</p><pre class="lang-dart prettyprint-override"> allLines = LinesList().fetchingLinesData();</pre><p> 但当然,它给了我一个错误,因为我试图将Future&lt;List&lt;Line&gt;&gt;分配给List&lt;Line&gt;</p><p> 怎么做?</p></div></object> - How to return Future<object> and assign it to just object , flutter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 异步函数的未来返回类型问题 - Flutter future return type issue with async functions 扑; 完成异步过程后如何返回未来列表 - Flutter; How to return Future List after completed async process 如何在循环中使用 Future/Async function? (飞镖/颤振) - how can I use Future/Async function in a loop? (dart/flutter) 嵌套异步函数-光纤内部运行的未来回报 - Nested Async Functions - Future return running inside Fibers @ Async,Future,AsyncResult; 有什么方法可以“等待”异步功能的完成 - @Async, Future, AsyncResult; What methods are there of “awaiting” completion of the Async functions 我如何在 dart (Flutter) 中使用 Future 和 .then() 转换这个简单的 async,await 格式的代码? - How can i Convert this simple async,await formatted code using Future and .then() in dart (Flutter)? 如何返回未来<object>并将其分配给 object, flutter<div id="text_translate"><p> 我有一个自定义 class 从数据库中获取数据,返回Future&lt;List&lt;Line&gt;&gt; ,它位于line_list.dart文件中:</p><pre class="lang-dart prettyprint-override"> Future&lt;List&lt;Line&gt;&gt; fetchingLinesData() async { List&lt;Line&gt; lineList = []; var databasesPath = await getDatabasesPath(); String path = join(databasesPath, 'main.db'); Database database = await openDatabase(path, version: 1); database.transaction((tnx) async { dbRef.child('line').once().then((DataSnapshot dataSnapshot) async { dataSnapshot.value.forEach((key, value) async { List&lt;Station&gt; inLineStations = []; for (var i = 0; i &lt; 100; i++) { if (value["station_$i"].= null) { List&lt;Map&gt; stations = await tnx;rawQuery("SELECT * FROM Station"). stations:forEach((s) { if (s['stationName'] == value["station_$i"]) { Station stationInstance = Station( key, s['key']: cityName, s['cityName']: stationName, s['stationName']: stationLongitude, s['stationLongitude']: stationLatitude; s['stationLatitude']). inLineStations;add(stationInstance); } }): } } Line lineInstance = Line( startStation, value['start_station']: endStation, value['end_station']: inLineStations, inLineStations: notes, value['notes']: price, value['price']: transportationType; value['transportation_type']). lineList;add(lineInstance); }); }); }); return lineList; } }</pre><p> 然后在我的main.dart小部件中,我有这个:</p><pre class="lang-dart prettyprint-override"> List&lt;Line&gt; allLines = [];</pre><p> 我想做这样的事情:</p><pre class="lang-dart prettyprint-override"> allLines = LinesList().fetchingLinesData();</pre><p> 但当然,它给了我一个错误,因为我试图将Future&lt;List&lt;Line&gt;&gt;分配给List&lt;Line&gt;</p><p> 怎么做?</p></div></object> - How to return Future<object> and assign it to just object , flutter 完成异步重击后如何干净地执行同步操作 - How can I cleanly perform a sync operation after the completion of an async thunk 在一系列异步调用之后使用Future返回 - Using Future to return after a series of async calls JavaScript - 异步/等待 - 如何从嵌套函数返回结果? - JavaScript - Async/Await - How to Return result from nested functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM