Reputation: 431
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.
Upvotes: 19
Views: 84465
Reputation: 9
List listItems = [];
getList() async {
listItems = await Future<List> listofFuture;
}
...
ElevatedButton(
onPressed: () {
setState(() {});},
child: Text('Update Data'));
Upvotes: 1
Reputation: 147
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)),);
});
Upvotes: -1
Reputation: 399
List<Future<dynamic>> _selectedItems = List<Future<dynamic>>();
List<dynamic> listofimg = [];
_selectedItems.forEach((element) {
element.then((value) => listofimg.add(value));
});
Upvotes: 0
Reputation: 938
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;
}
Upvotes: 2
Reputation: 5763
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.
Upvotes: 22
Reputation: 3716
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.
Upvotes: 4
Reputation: 628
assuming this is ur returning function dataList() which is Futur :
List yourlist = new List();
dataList().then((resultat){
setState(() => yourlist.add(resultat);
});
Upvotes: 1
Reputation: 268464
List list = await _fetchList();
Assuming _fetchList()
is something like:
Future<List> _fetchList() {...}
Upvotes: 16