I have a list of String addresses like:
List<String> addressStrings = [....];
I am using the geocoding
plugin to get the address data and marker for these address strings:
//This is a class-level function
Future<List<MarkerData>> getMarkerDataList() async {
List<MarkerData> list = [];
addressStrings.forEach((element) async {
final result = await locationFromAddress(element);
final markerData = MarkerData(element, result.first);
list.add(markerData);
});
return list;
}
But it freezes the UI as expected. I tried to use compute
to perform the operation in another isolate like:
//This is a top-level function
Future<List<MarkerData>> getMarkerDataList(List<String> addressStrings) async {
List<MarkerData> list = [];
addressStrings.forEach((element) async {
final result = await locationFromAddress(element);
final markerData = MarkerData(element, result.first);
list.add(markerData);
});
return list;
}
//This is a class-level function
Future<List<MarkerData>> getMarkerData()async{
final result = await compute(getMarkerDataList, addressStrings);
return result;
}
But it doesn't work and shows an Unhandled exception
in the console.
I guess the final result = await locationFromAddress(element);
request is the problem. Because it does pass before that statement but doesn't this one.
So, my question is: does compute
support async
? If yes, what am I doing wrong here? If not, how can I do asynchronous performance-intensive tasks like this efficiently without blocking the UI?