简体   繁体   English

如何在 flutter 的后台运行 dart 代码?

[英]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.

相关问题 由于这个错误,我无法运行 dart/flutter 代码。 有什么帮助吗? 我 - I can't run dart/flutter code due to this error. Any help? I 如何在后台运行此 Android 代码 - How can I run this Android code in Background 我如何在主要飞镖中运行图表? - 颤抖 - How do i run the charts in main dart? - flutter 如何在 Android 的后台线程上运行代码? - How can I run code on a background thread on Android? 安装新应用时如何在后台运行代码? - How can I run code in the background when a new app is installed? 如何将其他 dart 文件导入 flutter 中的有状态小部件? - How can I import other dart file to stateful widget in flutter? 如何在 Flutter/Dart 中请求短信权限? - How can I request sms permissions in Flutter/Dart? 如何在 dart flutter 中格式化时间字符串? - How can I format string of time in dart flutter? flutter 和 dart 的新手,通过创建世界时间应用程序来创建初学者项目,但是当我运行代码时会发生这种情况 - new to flutter and dart, creating a beginners project by creating a world time app but this happens when i run the code 如何使用 Flutter 背景代码以 5 秒为间隔截取屏幕截图? - How can I take screenshots on 5-second intervals with Flutter background code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM