[英]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.我想在我的应用程序的 android(Kotlin) 部分的 main.dart 文件中运行一个方法。 For this I am using methodChannels and it works perfectly fine when the app is in foreground.
为此,我正在使用 methodChannels,当应用程序处于前台时它工作得非常好。 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.
但是,在应用程序被杀死的那一刻,flutterView 被破坏,因此我无法在我的 main.dart 文件中运行任何方法。
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.我读到使用 FlutterNativeView 可以让我做我想做的事,但我找不到小代码片段来帮助我。
It would be great if you could help me with this如果你能帮我解决这个问题那就太好了
Thanks!!谢谢!!
In Flutter, you can execute Dart code in the background. 在Flutter中,可以在后台执行Dart代码。
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.
隔离是 Dart 用于多线程的 model,尽管隔离与传统线程的不同之处在于它不与主程序共享 memory。 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 . 有关更多信息和使用 Dart 代码的后台执行的地理围栏示例,请参阅 Ben Konyi 的中篇文章,使用 ZC047B10EEE763AFB6164E077CF1CC26 在后台执行 Dart 和地理围栏。 At the end of this article, you'll find links to example code, and relevant documentation for Dart, iOS, and Android.
在本文末尾,您将找到示例代码的链接,以及 Dart、iOS 和 Android 的相关文档。
You can write the native code for Firebase realtime database.您可以为 Firebase 实时数据库编写本机代码。 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.您应该创建一个自定义插件并将本机代码放入该插件中,因为将文件直接写入 Flutter
android
文件夹将使项目将来无法管理,您可能会遇到多个意外问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.