rmawatson / flutter_isolate

Launch an isolate that can use flutter plugins.
MIT License
262 stars 80 forks source link

Unhandled Exception: Null check operator used on a null value #141

Closed Wang-code-say closed 11 months ago

Wang-code-say commented 1 year ago

E/flutter (19743): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value E/flutter (19743): #0 FlutterIsolate._isolateInitialize.. (package:flutter_isolate/flutter_isolate.dart:141) E/flutter (19743): #1 _RootZone.runUnaryGuarded (dart:async/zone.dart:1593) E/flutter (19743): #2 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339) E/flutter (19743): #3 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271) E/flutter (19743): #4 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774) E/flutter (19743): #5 _StreamController._add (dart:async/stream_controller.dart:648) E/flutter (19743): #6 _StreamController.add (dart:async/stream_controller.dart:596) E/flutter (19743): #7 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192) E/flutter (19743): The above is the problem I encountered

Wang-code-say commented 1 year ago

@pragma('vm:entry-point') static void startWifiScan(StreamController<List?> wifiStreamController) async { final ReceivePort wifiReceivePort = ReceivePort(); await FlutterIsolate.spawn( wifiScanIsolate, wifiReceivePort.sendPort, ); wifiReceivePort.listen((data) { if (data is List && data.isNotEmpty) { final wifiList = data.map((serializedWifi) => WifiNetwork.fromJson(serializedWifi)).toList(); wifiStreamController.add(wifiList); } }); }

static void wifiScanIsolate(SendPort wifiSendPort) async { const Duration interval = Duration(seconds: 5); const String prefix = "admin"; await for (var wifiList in scanWifiNetworks(interval, prefix)) { final serializedList = wifiList.map((wifi) => wifi.toJson()).toList(); if (serializedList.isNotEmpty) { wifiSendPort.send(serializedList); } else { FlutterIsolate.current.kill(); } } }

static Stream<List> scanWifiNetworks(Duration interval, String prefix) async* { while (true) { List wifiList = await WiFiForIoTPlugin.loadWifiList(); Set ssidSet = {}; for (var element in wifiList) { if (element.ssid.toString().contains(prefix)) { ssidSet.add(element); } }

  yield ssidSet.toList();
  await Future.delayed(interval);
}

}

Wang-code-say commented 1 year ago

I have no problem running in my debug mode, but this error occurs in release mode. If it is not a component problem, please let me know.

nmfisher commented 1 year ago

@Wang-code-say can you provide a reproducible sample project?

Wang-code-say commented 12 months ago

@nmfisher Sorry, I just saw it. Here is an example I just created, thanks again for your reply https://github.com/Wang-code-say/flutter_test.git

Wang-code-say commented 11 months ago

@Wang-code-say can you provide a reproducible sample project?

hello ,waiting your response

nmfisher commented 11 months ago

You've added the @pragma('vm:entry-point') annotation to the wrong function. It needs to be on the isolate entrypoint function passed to FlutterIsolate.spawn (i.e. add it here https://github.com/Wang-code-say/flutter_test/blob/4ca5081b69b52dcfbd408620c62aeb4babe3c134/flutter_isolate_test/lib/main.dart#L131).

Wang-code-say commented 11 months ago

您已将@pragma('vm:entry-point')注释添加到错误的函数中。它需要定位传递到隔离入口点函数上FlutterIsolate.spawn(即在此处添加它https://github.com/Wang-code-say/flutter_test/blob/4ca5081b69b52dcfbd408620c62aeb4babe3c134/flutter_isolate_test /lib/main.dart#L131)。

ok ,thank you so much ,it's working ,i will close this issu