How to convert Future<List> to List in flutter?

狂风中的少年 提交于 2020-08-05 06:01:25

问题


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.


回答1:


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.




回答2:


List list = await _fetchList();

Assuming _fetchList() is something like:

Future<List> _fetchList() {...}



回答3:


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.




回答4:


assuming this is ur returning function dataList() which is Futur :

 List yourlist = new List();
   dataList().then((resultat){
          setState(() => yourlist.add(resultat); 
        });



回答5:


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;
  }


来源:https://stackoverflow.com/questions/59489064/how-to-convert-futurelist-to-list-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!