How to use Flutter Method Channel in background (app minimised/closed)

后端 未结 2 1267
滥情空心
滥情空心 2021-01-05 20:57

I am working on a native Android widget in a Flutter App. In which there is refresh button, on click of that I have to call a method in the Flutter code. I am using Flutter

相关标签:
2条回答
  • 2021-01-05 21:16

    I am collecting information/discussion that redirects us to run flutter engine in background.

    void callbackDispatcher() {
    WidgetsFlutterBinding.ensureInitialized();
    print("Our background job ran!");
    

    }

    void main() {
    
     static const MethodChannel _channel = const MethodChannel("channel-name");
    
     Future<void> initialize(final Function callbackDispatcher) async {
    
      final callback = PluginUtilities.getCallbackHandle(callbackDispatcher);
    
     await _channel.invokeMethod('initialize', callback.toRawHandle());
     }
    }
    

    As stated here How to run Flutter in the background?

    When a background job is started by native the Flutter engine is not active. So we are unable to run Dart. Can Android/iOS starts the Flutter engine in the background? Yes! We’ll first need to register a Dart callback function which will only be invoked whenever a background job is started by the native code. This callback function is referred to as a callbackDispatcher.

    Also please check out these stackoverflow discussions.

    Flutter : Run an app as a background service

    How to create a service in Flutter to make an app to run always in background?

    How to create a Flutter background service that works also when app closed

    Executing Dart in the Background with Flutter Plugins and Geofencing

    0 讨论(0)
  • 2021-01-05 21:17

    You may start the Flutter Engine in the background by register a Dart callback function which will only be invoked whenever a background job is started in Flutter.

    Try this. https://medium.com/vrt-digital-studio/flutter-workmanager-81e0cfbd6f6e

    0 讨论(0)
提交回复
热议问题