[英]How to convert Future<List> to List in flutter?
I am using a plugin for flutter called search_widget
.我正在使用一个名为
search_widget
颤振插件。 The data parameter of this widget takes a list.此小部件的数据参数采用列表。 But as I use
sqlite
for fetching data, I have it in Future<List>
form.但是当我使用
sqlite
获取数据时,我将它以Future<List>
形式存在。 Is there any way I can convert Future<List>
to List
?有什么办法可以将
Future<List>
转换为List
吗? Or any other way to get this working.或者任何其他方式来让这个工作。
List list = await _fetchList();
假设_fetchList()
是这样的:
Future<List> _fetchList() {...}
Using await
keyword will wait for Future to get completed and once your Future is executed it's result will be returned to you.使用
await
关键字将等待 Future 完成,一旦你的 Future 被执行,它的结果将返回给你。
import 'dart:async';
void main() async {
Future<List> _futureOfList = _getList();
List list = await _futureOfList ;
print(list); // will print [1, 2, 3, 4] on console.
}
Future<List> _getList(){
return Future.value([1,2,3,4]);
}
for this to work, the method where you are calling should be async
为此,您调用的方法应该是
async
I hope this helps, in case of any doubt please comment.我希望这会有所帮助,如有任何疑问,请发表评论。
Borrowing the example from search_widget you need dataList
in a widget like this:借用search_widget的示例,您需要像这样的小部件中的
dataList
:
SearchWidget<LeaderBoard>(
dataList: list,
textFieldBuilder: (TextEditingController controller, FocusNode focusNode) {
return MyTextField(controller, focusNode);
},
)
Sure, you can convert Future<List>
into List
like other answers suggest.当然,您可以像其他答案建议的那样将
Future<List>
转换为List
。 But you won't be able to do dataList: await _sqliteCall();
但是你不能做
dataList: await _sqliteCall();
because build
methods are designed to be pure and sychronous.因为
build
方法被设计为纯粹的和同步的。
While the Future completes you will have to return something like a progress indicator.当 Future 完成时,您将不得不返回诸如进度指示器之类的东西。 For that you can use a
FutureBuilder
:为此,您可以使用
FutureBuilder
:
FutureBuilder<List<Leaderboard>>(
future: _sqliteCall(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return SearchWidget<LeaderBoard>(
dataList: snapshot.data,
textFieldBuilder: (TextEditingController controller, FocusNode focusNode) {
return MyTextField(controller, focusNode);
},
)
}
return CircularProgressIndicator();
}
),
Of course this can also be done with a StatefulWidget
, you can checkthis article for a detailed explanation of the issue .当然,这也可以通过
StatefulWidget
来完成,您可以查看这篇文章以获取对该问题的详细说明。
assuming this is ur returning function dataList() which is Futur :假设这是你的返回函数 dataList() ,它是 Futur :
List yourlist = new List();
dataList().then((resultat){
setState(() => yourlist.add(resultat);
});
Thats how I have solved the problem...我就是这样解决问题的...
Initial version:初始版本:
@override
List<Product> getAll() {
List<Product> _listProducts;
Future<List<Product>> listFuture;
listFuture = _repo.getAll();
listFuture.then((value) {
if (value != null) value.forEach((item) => _listProducts.add(item));
});
return _listProducts == null ? [] : _listProducts;
}
Cleaned up/final version:清理/最终版本:
@override
List<Product> getAll() {
List<Product> _listProducts;
_repo.getAll().then((value) {
if (value != null) value.forEach((item) => _listProducts.add(item));
});
return _listProducts == null ? [] : _listProducts;
}
List<Future<dynamic>> _selectedItems = List<Future<dynamic>>();
List<dynamic> listofimg = [];
_selectedItems.forEach((element) {
element.then((value) => listofimg.add(value));
});
You should call value on {} of then as below:您应该在 {} of then 上调用 value ,如下所示:
onPressed: () {
_apiService.getProductList().then((result) {
print(result);
print("kkkkkkkk:"+result.length.toString());
for (var item in result){
_dsSanpham.add(item);
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => nhaphangle(
username: username, dshangle: dshangle, dssanpham: _dsSanpham)),);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.