0

I am trying to use isolates to load a list of waypoint, around 20k of them, I tried it the normal way takes around 1 to 2 mins to load, so thought of trying isolates, never used them, This is the code i have come up with so far,

Future<void> loadWayAirwayLinesMarkers() async {
    double markerWidth = 40;
    double markerHeight = 40;
    // Load the JSON file containing the markers
    final String jsonString =
        await rootBundle.loadString('assets/airway_waypoints.json');

    // Start an Isolate to parse the JSON and create the polyline data
    final completer = Completer<List<List<LatLng>>>();
    final isolate = await Isolate.spawn(parseJsonAndCreatePolylines,
        {'jsonString': jsonString, 'completer': completer});

    // Wait for the Isolate to complete and return the polyline data
    airwayPolyLine = await completer.future;

    // Cleanup by terminating the Isolate
    isolate.kill();

    notifyListeners();
  }

  void parseJsonAndCreatePolylines(Map<String, dynamic> data) {
    final String jsonString = data['jsonString'];
    final completer = data['completer'];

    final jsonResult = json.decode(jsonString);
    final List<List<LatLng>> result = [];

    jsonResult.forEach((key, value) {
      final List<LatLng> polylinePoints = [];
      for (var i = 0; i < value.length; i++) {
        final LatLng point =
            LatLng(value[i]['latitude'], value[i]['longitude']);
        polylinePoints.add(point);
      }
      result.add(polylinePoints);
    });

    completer.complete(result);
    // notifyListeners();
  }

Absolutely no idea if i am doing it correct, Throws this error though.

Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:ui' Class: Path)

And without isolates, this code worked fine, just a slow to load

 //List<List<LatLng> >airwayPolyLine = [];
  Future<void> loadWayAirwayLinesMarkers() async {
    double markerWidth = 40;
    double markerHeight = 40;
    // Load the JSON file containing the markers
    final String jsonString =
        await rootBundle.loadString('assets/airway_waypoints.json');
    // Decode the JSON data into a List of Maps
    //final List<dynamic> jsonData = await json.decode(jsonString);
    // Create a List of Markers from the JSON data
    final jsonResult = json.decode(jsonString);
    jsonResult.forEach((key, value) {
      final List<LatLng> polylinePoints = [];
      for (var i = 0; i < value.length; i++) {
        final LatLng point =
            LatLng(value[i]['latitude'], value[i]['longitude']);
        polylinePoints.add(point);
      }
      airwayPolyLine.add(polylinePoints);
      //notifyListeners();
    });

    notifyListeners();
  }
Pannam
  • 482
  • 1
  • 3
  • 16

0 Answers0