[英]How can I store a Future <dynamic> data for later use within a program in dart/Flutter?
I have the following code that does not work:我有以下代码不起作用:
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
main(){
String firstTitle = "";
firstTitle = logic().then((list) => getFirstTitle(list)); // I want to store this data into the variable
// firstTitle for later use.
// However my IDE does not recognize the second firstTitle as being linked to the declaration String firstTitle.
}
class MyList {
static var list = [1];
}
logic() async{
final result = await http.get('https://invidio.us/api/v1/search?q=tech+lead');
final data = json.decode(result.body);
final myList = [];
data.forEach((e) {
myList.add({"title": e['title'], 'videoId': e['videoId'], 'duration': e['lengthSeconds'], "date": e['publishedText']});
//print(myList);
});
return myList;
}
String getFirstTitle(aList){
return aList[0]["title"];
}
I understand that we await for the data to be fetched from the source but once it is how can I keep as any variable ex: String instead of having it as a Future.我知道我们在等待从源中获取数据,但是一旦它成为任何变量,例如:String,而不是将其作为 Future。
UPDATE: To better illustrate the problem with the IDE.更新:为了更好地说明 IDE 的问题。
Use async await.使用异步等待。
main() async {
String firstTitle = "";
List list=[];
list=await logic();
firstTitle = getFirstTitle(list));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.