gpt4 book ai didi

flutter - 使用 Flutter 的计算功能时出现 ServicesBinding.defaultBinaryMessenger 问题

转载 作者:行者123 更新时间:2023-12-03 04:43:47 24 4
gpt4 key购买 nike

我正在使用 flutter 构建一个资源管理器获取应用程序长度需要一段时间,所以我尝试使用另一个 来获取值隔离 ,这是我的 main.dart 代码的一部分

Future<int> getValueFromIsolate() async {
return await compute(
apps,
0,
);
}

Future<int> apps(int n) async {
int value = 0;
List apps = await DeviceApps.getInstalledApplications(
includeAppIcons: true,
includeSystemApps: true,
onlyAppsWithLaunchIntent: true,
);
print(apps.length);
value = apps.length;
print(value);
return value;
}
所以这是我的主要功能
void main() {
WidgetsFlutterBinding.ensureInitialized();
getValueFromIsolate().then(
(value) {
print("Then the value is $value");
},
);
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(
create: (_) => AppProvider(),
),
ChangeNotifierProvider(
create: (_) => CategoryProvider(),
),
ChangeNotifierProvider(
create: (_) => CoreProvider(),
),
],
child: MyApp(),
),
);
}
但我仍然得到这个错误

I/flutter ( 6234): ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.I/flutter ( 6234): If you're running an application and need to access the binary messenger before runApp() has been called (for example, during plugin initialization), then you need to explicitly call the WidgetsFlutterBinding.ensureInitialized() first.I/flutter ( 6234): If you're running a test, you can call the TestWidgetsFlutterBinding.ensureInitialized() as the first line in your test's main() method to initialize the binding.


我不明白发生了什么
我不知道该怎么办..!!
请需要您的帮助 感谢您的阅读和您的帮助

最佳答案

目前 Flutter 平台 channel 有限制 - 通信仅由应用程序启动时创建的主隔离支持。

  • DeviceApps.getInstalledApplications 确实调用平台 channel 来访问平台 API 和
  • 你从隔离中调用它 - compute()为您创建新的

  • However, there are two plugins to help you out:

    flutter_isolate provides areplacement isolate that can communicate to plugins because it createsits own UI backing (nothing that you see or have to deal with, justtechnically),

    isolate_handler Theadvantage in using this package rather than flutter_isolate itself isthat this adds handling capabilities, you can start several isolates,keep track of them and you don't have to set up your own communicationbetween the isolate and the main thread (something you have to domanually with both the original stock Isolate and FlutterIsolate)because it's abstracted away and readily available.


    如果您仍有疑问,为什么?或有兴趣深入了解 - read all discussion with Flutter developers in github issue

    关于flutter - 使用 Flutter 的计算功能时出现 ServicesBinding.defaultBinaryMessenger 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62649795/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com