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.
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.
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.
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.
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.
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.
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.
List list = await _fetchList();
Assuming _fetchList()
is something like:
Future<List> _fetchList() {...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With