I use isolate.spawnUri() in flutter and caused a fatal error

Issue

this is my cod:

  void callTask()async{
    Uri uri = Uri.parse('package:flutter_bedrock/base_framework/utils/isolate/factory/proxy_isolate.dart');

    final ReceivePort receivePort = ReceivePort();
    Isolate isolate = await Isolate.spawnUri(uri,
        [], receivePort.sendPort);
    SendPort childPort;
    receivePort.listen((message) {
      print('msg from proxy $message');
      if(message[0] == kSendPortKey){
        childPort = message[1];
        childPort.send([kTaskKey,'test']);
      }
    });



  }

I invoke callTask() at flutter, and log this :

E/flutter (14779): [ERROR:flutter/runtime/dart_isolate.cc(864)] Unhandled exception:
E/flutter (14779): error: native function 'Window_setNeedsReportTimings' (2 arguments) cannot be found
E/flutter (14779): #0      Window.onReportTimings= (dart:ui/window.dart:964:29)
E/flutter (14779): #1      SchedulerBinding.addTimingsCallback (package:flutter/src/scheduler/binding.dart:234:14)
E/flutter (14779): #2      SchedulerBinding.initInstances (package:flutter/src/scheduler/binding.dart:210:7)
E/flutter (14779): #3      PaintingBinding.initInstances (package:flutter/src/painting/binding.dart:21:11)
E/flutter (14779): #4      SemanticsBinding.initInstances (package:flutter/src/semantics/binding.dart:22:11)
E/flutter (14779): #5      RendererBinding.initInstances (package:flutter/src/rendering/binding.dart:30:11)
E/flutter (14779): #6      WidgetsBinding.initInstances (package:flutter/src/widgets/binding.dart:255:11)
E/flutter (14779): #7      new BindingBase (package:flutter/src/foundation/binding.dart:57:5)
E/flutter (14779): #8      new _WidgetsFlutterBinding&BindingBase&GestureBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #9      new _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #10     new _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #11     new _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #12     new _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #13     new _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #14     new _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #15     new WidgetsFlutterBinding (package:flutter/src/widgets/binding.dart)
E/flutter (14779): #16     WidgetsFlutterBinding.ensureInitialized (package:flutter/src/widgets/binding.dart:1203:7)
E/flutter (14779): #17     main (package:flutter_bedrock/main.dart:24:25)
E/flutter (14779): #18     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (14779): #19     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

in main isolate :


void main(){
   WidgetsFlutterBinding.ensureInitialized();
   IsolatePoolProxy.getInstance().callTask();
}


import 'dart:isolate';


const int kSendPortKey = 6633;

const int kTaskKey = 8844; 
///
const String kMethodName = 'kMethodName';
const String kNameArgs = 'kNameArgs';

class IsolatePoolProxy{

  IsolatePoolProxy._();

  static IsolatePoolProxy _instance;

  factory IsolatePoolProxy() => getInstance();

  static IsolatePoolProxy getInstance(){
    if(_instance == null){
      _instance = IsolatePoolProxy._();
    }
    return _instance;
  }


  void callTask()async{
    Uri uri = Uri.parse('package:flutter_bedrock/base_framework/utils/isolate/factory/proxy_isolate.dart');

    final ReceivePort receivePort = ReceivePort();
    Isolate isolate = await Isolate.spawnUri(uri,
        [], receivePort.sendPort);
    SendPort childPort;
    receivePort.listen((message) {
      print('msg from proxy $message');
      if(message[0] == kSendPortKey){
        childPort = message[1];
        childPort.send([kTaskKey,'test']);
      }
    });



  }


}

in child isolate at proxy_isolate.dart file :

import 'dart:async';
import 'dart:isolate';


const int kSendPortKey = 6633;

const int kTaskKey = 8844; 

///
const String kMethodName = 'kMethodName';
const String kNameArgs = 'kNameArgs';



final List<String> orders = [];

void main( args,SendPort mainPort)async{

  ///connect with main isolate
  final ReceivePort receivePort = ReceivePort();
  final SendPort sendPort = receivePort.sendPort;
  receivePort.listen((message) {
    print('msg from main  $message');
    if(message[0] == kTaskKey){
      orders.add(message[1]);
    }

  });

  mainPort.send([kSendPortKey,sendPort]);

}

E/flutter (14779): [ERROR:flutter/runtime/dart_isolate.cc(864)] Unhandled exception:
E/flutter (14779): error: native function ‘Window_setNeedsReportTimings’ (2 arguments) cannot be found

Solution

You cannot use dart:ui in isolates.

See this issue on the flutter github.

There does not seem to be a workaround. You cannot do it that way, find another way to do what you want to do.

Answered By – nvoigt

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.