flutternetwork / WiFiFlutter

Flutter plugin suite for various WiFi services.
https://wifi.flutternetwork.dev
289 stars 179 forks source link

Android application crashed with running scan from isolate #67

Closed easyterm-devel closed 4 years ago

easyterm-devel commented 4 years ago

Based on skeleton: https://gist.github.com/jebright/a7086adc305615aa3a655c6d8bd90264

  static void _scanAP(SendPort sendPort) async {
    Timer.periodic(new Duration(seconds: 5), (Timer t) async {
    List<WifiNetwork> scanResult;
      try {
        scanResult = await WiFiForIoTPlugin.loadWifiList();
      } on PlatformException {
        scanResult = List<WifiNetwork>();
      }
      _counter++;
    });
  }

another approach not working properly:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
  .then((_) {
  runApp(new MyApp());
 });
}
flutter doctor -v
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Linux, locale en_US.UTF-8)
    • Flutter version 1.12.13+hotfix.9 at /home/aia/flutter
    • Framework revision f139b11009 (3 weeks ago), 2020-03-30 13:57:30 -0700
    • Engine revision af51afceb8
    • Dart version 2.7.2

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /home/aia/Android/Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /snap/android-studio/88/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Android Studio (version 3.6)
    • Android Studio at /snap/android-studio/88/android-studio
    • Flutter plugin version 45.0.1
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] Connected device (1 available)
    • 0123456789ABCDEF • 0123456789ABCDEF • android-arm • Android 4.1.1 (API 16)

• No issues found!
$ flutter run
Launching lib/main.dart on 0123456789ABCDEF in debug mode...
D/FlutterActivity( 7121): Using the launch theme as normal theme.       
D/FlutterActivityAndFragmentDelegate( 7121): Setting up FlutterEngine.  
D/FlutterActivityAndFragmentDelegate( 7121): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
D/FlutterActivityAndFragmentDelegate( 7121): Attaching FlutterEngine to the Activity that owns this Fragment.
D/FlutterView( 7121): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@4231d4c8
D/FlutterActivityAndFragmentDelegate( 7121): Executing Dart entrypoint: main, and sending initial route: /
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 7121): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Bad state: Stream has already been listened to.
E/flutter ( 7121): #0      _StreamController._subscribe (dart:async/stream_controller.dart:670:7)
E/flutter ( 7121): #1      _ControllerStream._createSubscription (dart:async/stream_controller.dart:820:19)
E/flutter ( 7121): #2      _StreamImpl.listen (dart:async/stream_impl.dart:474:9)
E/flutter ( 7121): #3      _ReceivePortImpl.listen (dart:isolate-patch/isolate_patch.dart:87:31)
E/flutter ( 7121): #4      _MyHomePageState._start (package:demo/main.dart:48:18)
E/flutter ( 7121): <asynchronous suspension>                            
E/flutter ( 7121): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter ( 7121): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter ( 7121): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 7121): #8      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter ( 7121): #9      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter ( 7121): #10     BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter ( 7121): #11     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 7121): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter ( 7121): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 7121): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 7121): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 7121): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 7121): #17     _rootRunUnary (dart:async/zone.dart:1138:13) 
E/flutter ( 7121): #18     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 7121): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 7121): #20     _invoke1 (dart:ui/hooks.dart:273:10)         
E/flutter ( 7121): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter ( 7121):                                                      
E/flutter ( 7121): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Bad state: Stream has already been listened to.
E/flutter ( 7121): #0      _StreamController._subscribe (dart:async/stream_controller.dart:670:7)
E/flutter ( 7121): #1      _ControllerStream._createSubscription (dart:async/stream_controller.dart:820:19)
E/flutter ( 7121): #2      _StreamImpl.listen (dart:async/stream_impl.dart:474:9)
E/flutter ( 7121): #3      _ReceivePortImpl.listen (dart:isolate-patch/isolate_patch.dart:87:31)
E/flutter ( 7121): #4      _MyHomePageState._start (package:demo/main.dart:48:18)
E/flutter ( 7121): <asynchronous suspension>                            
E/flutter ( 7121): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter ( 7121): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter ( 7121): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 7121): #8      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter ( 7121): #9      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter ( 7121): #10     BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter ( 7121): #11     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 7121): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter ( 7121): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 7121): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 7121): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 7121): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 7121): #17     _rootRunUnary (dart:async/zone.dart:1138:13) 
E/flutter ( 7121): #18     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 7121): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 7121): #20     _invoke1 (dart:ui/hooks.dart:273:10)         
E/flutter ( 7121): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter ( 7121):                                                      
E/flutter ( 7121): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Bad state: Stream has already been listened to.
E/flutter ( 7121): #0      _StreamController._subscribe (dart:async/stream_controller.dart:670:7)
E/flutter ( 7121): #1      _ControllerStream._createSubscription (dart:async/stream_controller.dart:820:19)
E/flutter ( 7121): #2      _StreamImpl.listen (dart:async/stream_impl.dart:474:9)
E/flutter ( 7121): #3      _ReceivePortImpl.listen (dart:isolate-patch/isolate_patch.dart:87:31)
E/flutter ( 7121): #4      _MyHomePageState._start (package:demo/main.dart:48:18)
E/flutter ( 7121): <asynchronous suspension>                            
E/flutter ( 7121): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14)
E/flutter ( 7121): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36)
E/flutter ( 7121): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 7121): #8      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11)
E/flutter ( 7121): #9      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5)
E/flutter ( 7121): #10     BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:236:7)
E/flutter ( 7121): #11     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 7121): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter ( 7121): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 7121): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 7121): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 7121): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 7121): #17     _rootRunUnary (dart:async/zone.dart:1138:13) 
E/flutter ( 7121): #18     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 7121): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 7121): #20     _invoke1 (dart:ui/hooks.dart:273:10)         
E/flutter ( 7121): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5)
E/flutter ( 7121):                                                      
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 7121): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 7121): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 7121): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 7121): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 7121): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 7121): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 7121): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 7121): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 7121): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 7121): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 7121): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 7121): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 7121): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 7121): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
D/FlutterActivity( 8025): Using the launch theme as normal theme.       
D/FlutterActivityAndFragmentDelegate( 8025): Setting up FlutterEngine.  
D/FlutterActivityAndFragmentDelegate( 8025): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
D/FlutterActivityAndFragmentDelegate( 8025): Attaching FlutterEngine to the Activity that owns this Fragment.
D/FlutterView( 8025): Detaching from a FlutterEngine: null              
D/FlutterView( 8025): Not attached to an engine. Doing nothing.         
D/FlutterView( 8025): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@42314ee0
D/FlutterActivityAndFragmentDelegate( 8025): Executing Dart entrypoint: main, and sending initial route: /
D/FlutterView( 8025): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@42314ee0
D/FlutterView( 8025): Already attached to this engine. Doing nothing.   
D/FlutterView( 8025): Detaching from a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@42314ee0
D/FlutterActivityAndFragmentDelegate( 8025): Detaching FlutterEngine from the Activity that owns this Fragment.
D/FlutterEngine( 8025): Destroying.                                     
D/FlutterEnginePluginRegistry( 8025): Destroying.                       
Running Gradle task 'assembleDebug'...                                  
Running Gradle task 'assembleDebug'... Done                         3.6s
✓ Built build/app/outputs/apk/debug/app-debug.apk.
D/FlutterActivity( 8200): Using the launch theme as normal theme.
D/FlutterActivityAndFragmentDelegate( 8200): Setting up FlutterEngine.
D/FlutterActivityAndFragmentDelegate( 8200): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
D/FlutterActivityAndFragmentDelegate( 8200): Attaching FlutterEngine to the Activity that owns this Fragment.
D/FlutterView( 8200): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@42315d18
D/FlutterActivityAndFragmentDelegate( 8200): Executing Dart entrypoint: main, and sending initial route: /

Syncing files to device 0123456789ABCDEF...                             
(This is taking an unexpectedly long time.)       ⣻
(This is taking an unexpectedly long time.)       ⡿
(This is taking an unexpectedly long time.)       ⣯
12,023ms (!)                                       

🔥  To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on 0123456789ABCDEF is available at: http://127.0.0.1:44159/2a6fIhV7kgE=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

E/flutter ( 8200): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 8200): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 8200): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 8200): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 8200): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 8200): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 8200): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 8200): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 8200): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 8200): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 8200): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 8200): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 8200): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 8200): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

E/flutter ( 8200): [ERROR:flutter/runtime/dart_isolate.cc(915)] Unhandled exception:
E/flutter ( 8200): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 8200): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 8200): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 8200): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 8200): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 8200): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 8200): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 8200): #4      WiFiForIoTPlugin._loadWifiList (package:wifi_iot/wifi_iot.dart:155:32)
E/flutter ( 8200): #5      WiFiForIoTPlugin.loadWifiList (package:wifi_iot/wifi_iot.dart:164:39)
E/flutter ( 8200): #6      _MyHomePageState._scanAP.<anonymous closure> (package:demo/main.dart:57:45)
E/flutter ( 8200): #7      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 8200): #8      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 8200): #9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

Application finished.
easyterm-devel commented 4 years ago

full example:

import 'dart:async';
import 'package:flutter/material.dart';
import 'dart:isolate';

import 'package:flutter/services.dart';
import 'package:wifi_iot/wifi_iot.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
  .then((_) {
  runApp(new MyApp());
 });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Isolate Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Isolates'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Isolate _isolate;
  bool _running = false;
  static int _counter = 0;
  String notification = "";
  ReceivePort _receivePort;

  void _start() async {
    _running = true;
    _receivePort = ReceivePort();
    _isolate = await Isolate.spawn(_scanAP, _receivePort.sendPort);
    _receivePort.listen(_handleMessage, onDone:() {
        print("done!");
    });
  }

  static void _scanAP(SendPort sendPort) async {
    Timer.periodic(new Duration(seconds: 5), (Timer t) async {
    List<WifiNetwork> scanResult;
      try {
        scanResult = await WiFiForIoTPlugin.loadWifiList();
        //sendPort.send(scanResult.toString());
      } on PlatformException {
        scanResult = List<WifiNetwork>();
      }
      _counter++;
//      String msg = 'notification ' + _counter.toString() + scanResult.toString(); 
//      print('SEND: ' + msg);
//      sendPort.send(msg);
    });
  }

  void _handleMessage(dynamic data) {
    print('RECEIVED: ' + data);
    setState(() {      
      notification = data;
    });
  }

  void _stop() {
    if (_isolate != null) {
      setState(() {
          _running = false; 
          notification = '';   
      });
      _receivePort.close();
      _isolate.kill(priority: Isolate.immediate);
      _isolate = null;        
      }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              notification,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _running ? _stop : _start,
        tooltip: _running ? 'Timer stop' : 'Timer start',
        child: _running ? new Icon(Icons.stop) : new Icon(Icons.play_arrow),
      ),
      );
  }
}

Future<List<WifiNetwork>> loadWifiList() async {
  List<WifiNetwork> htResultNetwork;
  try {
    htResultNetwork = await WiFiForIoTPlugin.loadWifiList();
  } on PlatformException {
    htResultNetwork = List<WifiNetwork>();
  }
    return htResultNetwork;
}
easyterm-devel commented 4 years ago

Probably I forgot think about initializing part:

  @override
  initState() {
    super.initState();
    WiFiForIoTPlugin.setEnabled(true);
  }

but with apply method above not resolved too..

shashikantdurge commented 4 years ago

Platform APIs have to be called from main Isolate.