I am using a plugin for flutter called 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.
Is there any way I can convert Future<List>
to List
?
Or any other way to get this working.

- 237,138
- 77
- 654
- 440

- 391
- 1
- 4
- 11
8 Answers
Using await
keyword will wait for Future to get completed and once your Future is executed it's result will be returned to you.
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
I hope this helps, in case of any doubt please comment.

- 5,413
- 4
- 22
- 32
List list = await _fetchList();
Assuming _fetchList()
is something like:
Future<List> _fetchList() {...}

- 237,138
- 77
- 654
- 440
Borrowing the example from search_widget you need dataList
in a widget like this:
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. But you won't be able to do dataList: await _sqliteCall();
because build
methods are designed to be pure and sychronous.
While the Future completes you will have to return something like a progress indicator. For that you can use a 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 check this article for a detailed explanation of the issue.

- 3,386
- 1
- 16
- 16
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;
}

- 748
- 6
- 14
assuming this is ur returning function dataList() which is Futur :
List yourlist = new List();
dataList().then((resultat){
setState(() => yourlist.add(resultat);
});

- 628
- 4
- 12
List<Future<dynamic>> _selectedItems = List<Future<dynamic>>();
List<dynamic> listofimg = [];
_selectedItems.forEach((element) {
element.then((value) => listofimg.add(value));
});

- 319
- 2
- 7
-
Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit](https://stackoverflow.com/posts/64980911/edit) your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Nov 24 '20 at 08:57
List listItems = [];
getList() async {
listItems = await Future<List> listofFuture;
}
...
ElevatedButton(
onPressed: () {
setState(() {});},
child: Text('Update Data'));

- 1
- 1
future return of apiservice type (Future list). we use key word (then) then future type store to var result as list.
You should call value on {} of then as below:
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)),);
});

- 137
- 1
- 4