当我尝试使用Hive与path_provider时,我在我的Flutter项目中有此错误。是否有任何方法来解决这个问题,因为我尝试在beta和主通道之间切换,并将flutter sdk min设置为2.11。
当我调用path_provider.getApplicationDocumentsDirectory();
Unhandled Exception: Null check operator used on a null value
#0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:121:86)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:146:36)
#2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
#3 MethodChannelPathProvider.getApplicationDocumentsPath (package:path_provider_platform_interface/src/method_channel_path_provider.dart:50:10)
#4 getApplicationDocumentsDirectory (package:path_provider/path_provider.dart:138:40)
#5 initDatabase (package:translator/main.dart:16:9)
#6 main (package:translator/main.dart:11:9)
#7 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:145:25)
#8 _rootRun (dart:async/zone.dart:1428:13)
#9 _CustomZone.run (dart:async/zone.dart:1328:19)
#10 _runZoned (dart:async/zone.dart:1863:10)
#11 runZonedGuarded (dart:async/zone.d<…>
下面是我的设置:
environment:
sdk: ">=2.12.0 <3.0.0"
...
dependencies
hive: ^2.0.4
hive_flutter: ^1.1.0
path_provider: ^2.0.2
经过搜索,我发现这将解决问题:
WidgetsFlutterBinding.ensureInitialized; <--- problem here, missing()
WidgetsFlutterBinding.ensureInitialized();
Directory document = await getApplicationDocumentsDirectory();