简体   繁体   中英

How can I run dart code in the background in flutter?

I want to run a method in my main.dart file from the android(Kotlin) part of my app. For this I am using methodChannels and it works perfectly fine when the app is in foreground. However, the moment the app is killed, the flutterView is destroyed and therefore I am unable to run any methods in my main.dart file.

I read that using FlutterNativeView would allow me to do what I want but I am unable to find small code snippets to help me with it.

It would be great if you could help me with this

Thanks!!

In Flutter, you can execute Dart code in the background.

The mechanism for this feature involves setting up an isolate. Isolates are Dart's model for multithreading, though an isolate differs from a conventional thread in that it doesn't share memory with the main program. You'll set up your isolate for background execution using callbacks and a callback dispatcher.

For more information and a geofencing example that uses background execution of Dart code, see the Medium article by Ben Konyi, Executing Dart in the Background with Flutter Plugins and Geofencing . At the end of this article, you'll find links to example code, and relevant documentation for Dart, iOS, and Android.

You can write the native code for Firebase realtime database. You can check this .

You should create a custom plugin and put the native code in that plugin as writing the files directly in the Flutter android folder will make the project unmanageable in the future and you can face multiple unexpected issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM