3

I want to fetch data from native side to flutter in background service call. I have implemented flutter_background_service to work with foreground and background tasks at flutter side.

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((event) {
  service.setAsForegroundService();
});
service.on('setAsBackground').listen((event) {
  service.setAsBackgroundService();
});
}service.on('stopService').listen((event) {
service.stopSelf();
});
Timer.periodic(const Duration(seconds: 5), (timer) async {
if (service is AndroidServiceInstance) {
    if (await service.isForegroundService()) {
      print('FLUTTER FOREGROUND SERVICE: ${DateTime.now()}');
      WidgetsFlutterBinding.ensureInitialized();
      try {
        await bgMethodChannel.invokeMethod(bgMethod);
      } catch (e) {
        if (kDebugMode) {
          print("EXCEPTION: $e");
        }
      }
    }
}
});
}

print statement is getting printed in every 5 seconds in foreground but method channel call is throwing exception : MissingPluginException(No implementation found for method bg_method on channel inhalerChannelBG)

await bgMethodChannel.invokeMethod(bgMethod);

this line is working everywhere else but not inside onStart. I have used it calling in main() , that gives me results from android native side.

Can anyone help me out?

Shriya Pandya
  • 404
  • 3
  • 12
  • Hello @shriya, did you find any alternative way to fix this? – Pathik Patel Apr 24 '23 at 13:01
  • `flutter_background_service` is breaking many plugins in Flutter. I am experiencing it with SpotifySDK and getting the same `MissingPluginException` in iOS only, Android working fine. I think they are using the same channel/event that's why... – Muhammad Hassan May 03 '23 at 20:02

2 Answers2

0

try to change the type of the onStart method from void to Future<void> and call it like this await onStart(...) to make sure that it is fully initialized.

  • Thanks for reply. But I am using onStart in service configurations so can't use await here. await service.configure( androidConfiguration: AndroidConfiguration( onStart: onStart, autoStart: true, isForegroundMode: true, ), iosConfiguration: IosConfiguration( autoStart: true, onForeground: onStart, ), ); – Shriya Pandya Mar 07 '23 at 10:39
  • It didn't work @mohamedaminehnioua – Shriya Pandya Mar 07 '23 at 10:44
0

This issue has been solved by adding DartPluginRegistrant.ensureInitialized(); after this line void onStart(ServiceInstance service) async {

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
DartPluginRegistrant.ensureInitialized(); //add this 
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((event) {
  service.setAsForegroundService();
});
service.on('setAsBackground').listen((event) {
  service.setAsBackgroundService();
});
}service.on('stopService').listen((event) {
service.stopSelf();
});
Timer.periodic(const Duration(seconds: 5), (timer) async {
if (service is AndroidServiceInstance) {
    if (await service.isForegroundService()) {
      print('FLUTTER FOREGROUND SERVICE: ${DateTime.now()}');
      WidgetsFlutterBinding.ensureInitialized();
      try {
        await bgMethodChannel.invokeMethod(bgMethod);
      } catch (e) {
        if (kDebugMode) {
          print("EXCEPTION: $e");
        }
      }
    }
}
});
}
wuuyungwuu
  • 83
  • 13
  • It does not work with DartPluginRegistrant.ensureInitialized(); Can you tell me what could i change at native android side to access it through method channel. – Shriya Pandya Apr 05 '23 at 08:35
  • I see. Could you share your `pubspec.yaml` please? Since you are using android for your background service, have you added one of the [dependencies](https://pub.dev/packages/flutter_background_service_android)? – wuuyungwuu Apr 06 '23 at 01:04