ajinasokan / flutter_fgbg

Flutter plugin to detect when app(not Flutter container) goes to background or foreground
MIT License
81 stars 34 forks source link

MissingPluginException(No implementation found for method listen on channel com.ajinasokan.flutter_fgbg/events) #15

Closed lukehutch closed 1 month ago

lukehutch commented 1 month ago

I tried incorporating flutter_fgbg into my project, and on initialization I get this error:

════════ Exception caught by services library ══════════════════════════════════
The following MissingPluginException was thrown while activating platform stream on channel com.ajinasokan.flutter_fgbg/events:
MissingPluginException(No implementation found for method listen on channel com.ajinasokan.flutter_fgbg/events)

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

This seems to be the issue:

https://github.com/flutter/flutter/issues/98473#issuecomment-1058500136

The longer-term solution is that anything setting up an isolate where plugins are expected to work should call DartPluginRegistrant.ensureInitialized(), which will run all Dart plugin registration (similar to how they already need to call WidgetsFlutterBinding.ensureInitialized() to make platform message work in the first place).

lukehutch commented 1 month ago

This spontaneously fixed itself after flutter clean. You may want to suggest that users do a flutter clean after adding flutter_fgbg to their pubspec.yaml.