Skip to content

not working in isolate #5

@marcocatelli

Description

@marcocatelli

Hi, first of all thank you for your precious work.
I would like to run the discover inside an isolate so that it doesn't keep the main thread busy, but is not working.

This is the error:
Exception has occurred.
_AssertionError ('package:flutter/src/services/platform_channel.dart': Failed assertion: line 554 pos 7: '_binaryMessenger != null || BindingBase.debugBindingType() != null': Cannot set the method call handler before the binary messenger has been initialized. This happens when you call setMethodCallHandler() before the WidgetsFlutterBinding has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() before this or by passing a custom BinaryMessenger instance to MethodChannel().)

and this is the code of my isolate:

void searchServerIsolate(SendPort sp) async {
sp.send(IsolateStatus.working);
ZeroConnect zc = ZeroConnect();
Set advertisers = await zc.scan(
serviceId: Const.serviceId,
time: const Duration(seconds: 3),
);
List newServerList = [];
for (Ad advertiser in advertisers) {
final sock =
await Socket.connect(advertiser.addresses.first, Const.serverPort);
sock.write('requestName');
await sock.flush();
await for (Uint8List data in sock) {
String serverName = String.fromCharCodes(data);
Bc2pcServer server = Bc2pcServer(
name: serverName,
hostName: advertiser.addresses.first,
available: true);
if (newServerList.firstWhereOrNull((test) => test.name == serverName) ==
null) {
newServerList.add(server);
}
}
}
sp.send(IsolateStatus.paused);
sp.send(newServerList);
}

WidgetsFlutterBinding.ensureInitialized() has been called in the main thread but the isolate is another thread and there is no option to call WidgetsFlutterBinding.ensureInitialized() inside the isolate because it must be called from within the UI thread.

Is there a solution to this? Do you have something for me to try in mind?

Ciao, thank you!

Marco

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions