Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 696
Aether Avatar asked Dec 26 '19 13:12

Aether


People also ask

How do you deal with Future in Flutter?

Sometimes you don't want to turn the function into a Future or mark it async, so the other way to handle a Future is by using the . then function. It takes in a function that will be called with the value type of your Future. It's similar to a Promise in JavaScript without the resolve, reject explicitness.

How do I convert a string to a list in Flutter?

In Flutter and Dart, you can turn a given string into a list (with string elements) by using the split() method. To convert a list of strings to a string, you can use the join() method.

Why can't I use future in flutter getlist?

The flutter code part seems can't accept a Future. Show activity on this post. You should rewrite your getList method to return a Future<List>. It's possible to do this with a chain of then () invocations, but far more readable IMO to use async / await.

What is the difference between map() and tolist() in flutter?

It is a shallow copy of a list. In dart and flutter, this example converts a list of dynamic types to a list of Strings. map () is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString () is used. Finally, use the toList () method to return a list.

Is it possible to convert Future<list> to a list?

Borrowing the example from search_widget you need dataList in a widget like this: 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.

What is a future in flutter/Dart?

Long-running tasks are common in mobile apps. The way this is handled in Flutter / Dart is by using a Future. A Future allows you to run work asynchronously to free up any other threads that should not be blocked. Like the UI thread. A future is defined exactly like a function in dart, but instead of void you use Future.


1 Answers

List list = await _fetchList();

Assuming _fetchList() is something like:

Future<List> _fetchList() {...}
like image 115
CopsOnRoad Avatar answered Oct 15 '22 18:10

CopsOnRoad