Home > OS >  Mapping large data into list in Dart/Flutter Asynchronously
Mapping large data into list in Dart/Flutter Asynchronously

Time:11-19

I have a big list of data represented in a JSON-like object. It's an Iterable<Map<String, dynamic>> acquired from an SQLite database.

Running the query and even filtering the resulting data runs asynchronously, but when I try to map this data into a class, the Main Thread freezes.

The whole block of code is in an async function, and it returns a future of data, but for some reason, toList only runs synchronously.

I tried to use compute, but it keeps saying

Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function '<anonymous closure>':.)

I even tried RxDart's asyncMap, but that didn't seem to work any differently.

Trying to avoid function and use a normal for didn't work either.

What can I do to get the mapping code to run asynchronously?

This is the exact code that has the issue

    final filteredCards = cardsRaw.where((e) {
      return csvData.any(
        (element) =>
            e['card_name'] == element.cardName &&
            (e['number'] as String).startsWith(element.cardNumber) &&
            e['set_name'] == element.setName,
      );
    }).toList();

cardsRaw is 11k, and csvData is 1000

CodePudding user response:

Dart is single threaded, this means that even an async call will freeze the main thread if it has an expensive calculation.

The correct way of doing it is to spawn an isolate using compute like you tried, but compute can only take top-level (functions declared outside of a class) or static functions, that's the reason for your error.

Flutter has a handy cookbook that explains how to parse JSON in the background:

You can remove the jank by moving the parsing and conversion to a background isolate using the compute() function provided by Flutter. The compute() function runs expensive functions in a background isolate and returns the result.

  • Related