gpt4 book ai didi

android - 在 flutter 中使用 path_provider 包时出错

转载 作者:行者123 更新时间:2023-12-04 11:51:39 25 4
gpt4 key购买 nike

我正在尝试使用 flutter 中的 path_provider 包获取应用程序文档目录。我正在使用 hive 数据库,所以我需要路径。
这是我的代码:

void main() async {
final appDocsDir = await getApplicationDocumentsDirectory(); //error is on this line
Hive.init(appDocsDir.path);
runApp(MyApp());
}
我收到此错误:
E/flutter (18811): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (18811): #0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
E/flutter (18811): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
E/flutter (18811): #2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
E/flutter (18811): #3 MethodChannelPathProvider.getApplicationDocumentsPath (package:path_provider_platform_interface/src/method_channel_path_provider.dart:50:10)
E/flutter (18811): #4 getApplicationDocumentsDirectory (package:path_provider/path_provider.dart:138:40)
E/flutter (18811): #5 main (package:my_app/main.dart:9:28)
E/flutter (18811): #6 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter (18811): #7 _rootRun (dart:async/zone.dart:1354:13)
E/flutter (18811): #8 _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (18811): #9 _runZoned (dart:async/zone.dart:1789:10)
E/flutter (18811): #10 runZonedGuarded (dart:async/zone.dart:1777:12)
E/flutter (18811): #11 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:138:5)
E/flutter (18811): #12 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (18811): #13 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter (18811):

我正在通过 USB 连接的 android 设备上运行该应用程序(使用 flutter 运行)。我正在使用最新版本的 flutter 和软件包。
我怎样才能解决这个问题?
谢谢你!

最佳答案

我使用 Hive.initFlutter() 解决了这个问题.这是代码:

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';


void main() async {
await Hive.initFlutter()
runApp(MyApp());
}

关于android - 在 flutter 中使用 path_provider 包时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67692095/

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