Home > Software engineering >  Async .sort() in dart
Async .sort() in dart

Time:05-20

I am working with dart/flutter, and i need to sort large values ​​(10,000, 20,000 90,000 100,000 items ) and the application crashes for a few seconds due to the volume of very large data being processed. my doubt is: Is there any way to use the sort() function as async, to show the loadingProgressIndicator() when the data is sorte?

like this:

listFilt.sort((a,b)=> a.compareTo(b))

And i want to tranform in something like:

 showCircularIndicator = true;
 await listFilt.sort((a,b)=> a.compareTo(b));
 showCircularIndicator = false;

I know that sort() is synchronous function but have any way to transorm in asynchronous function?

CodePudding user response:

Use compute, here is the example of sorting photos according to title.

List<Photo> sortPhotos(List<Photo> photos) {
  photos.sort((a,b) => a.title.compareTo(b.title));
  return photos;
}

...
final List<Photo> photos = await ...
final result = await compute(sortPhotos, photos);
  • Related