[英]Error using path_provider package in flutter
I am trying to get the application documents directory using the path_provider package in flutter.我正在尝试使用 flutter 中的 path_provider package 获取应用程序文档目录。 I am using the hive database and so I need the path.我正在使用 hive 数据库,所以我需要路径。
Here's my code:这是我的代码:
void main() async {
final appDocsDir = await getApplicationDocumentsDirectory(); //error is on this line
Hive.init(appDocsDir.path);
runApp(MyApp());
}
I am getting this error:我收到此错误:
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):
I am running the app on an android device connected through usb (Using flutter run).我正在通过 usb 连接的 android 设备上运行该应用程序(使用 flutter 运行)。 I am using the latest version of flutter and the packages.我正在使用最新版本的 flutter 和软件包。
How can I fix this?我怎样才能解决这个问题?
Thanks You!感谢您!
try making these calls in the initState of the MyApp Widget.尝试在 MyApp Widget 的 initState 中进行这些调用。
like this:像这样:
initDocs() async {
final appDocsDir = await getApplicationDocumentsDirectory();
Hive.init(appDocsDir.path);
}
@override
void initState() {
initDocs();
super.initState();
}
or:或者:
@override
void initState() {
getApplicationDocumentsDirectory().then((e)=>Hive.init(e.path));
super.initState();
}
I solved the problem using Hive.initFlutter()
.我使用Hive.initFlutter()
解决了这个问题。 Here's the code:这是代码:
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
void main() async {
await Hive.initFlutter()
runApp(MyApp());
}
Just copy and paste this code, before: await getApplicationDocumentsDirectory();只需复制并粘贴此代码,之前:await getApplicationDocumentsDirectory();
WidgetsFlutterBinding.ensureInitialized();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.