DinhNam99 / usb_plugin

This is a plugin use Flutter Framework about USB
BSD 3-Clause "New" or "Revised" License
1 stars 2 forks source link

MissingPluginException #1

Open allasca opened 10 months ago

allasca commented 10 months ago
detectUSB() async {
    usbPlugin.stateUsbStream().listen((int data) async {
      print('The state of USB Connected: $data');
    });
  }

result

The following MissingPluginException was thrown while activating platform stream on channel usb_plugin/state_usb:
MissingPluginException(No implementation found for method listen on channel usb_plugin/state_usb)

When the exception was thrown, this was the stack:
#0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:320:7)
platform_channel.dart:320
<asynchronous suspension>
#1      EventChannel.receiveBroadcastStream.<anonymous closure> (package:flutter/src/services/platform_channel.dart:664:9)
platform_channel.dart:664
<asynchronous suspension>
beanomen4 commented 1 month ago

import 'dart:async'; and

void detectUSB() async {
    usbPlugin.stateUsbStream().listen((int data) async {
      print('The state of USB Connected: $data');
    });
  }

and

void initState() async {
    detectUSB();
    super.initState();
  }
beanomen4 commented 1 month ago

Also, every audition should be closed this way

@override
  void dispose() {
    usbSubscription?.cancel();
    super.dispose();
  }

and

StreamSubscription? usbSubscription;
...
void detectUSB() async {
    usbSubscription = usbPlugin.stateUsbStream().listen((int data) async {
      print('The state of USB Connected: $data');
    });
  }
DinhNam99 commented 1 month ago

Thanks @beanomen4 @allasca . I will update it