简体   繁体   English

如何解决 getit 共享首选项错误?

[英]how to solve getit shared preferences error?

I'm trying bloctesting for my project and getting the following error:我正在为我的项目尝试 bloctesting 并收到以下错误:

 'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7: 'instanceFactory:= null'. Object/factory with type SharedPreferences is not registered inside GetIt. (Did you accidentally do GetIt sl=GetIt;instance(). instead of GetIt sl=GetIt;instance? Did you forget to register it )

Below are my sharedpreferences injection files以下是我的 sharedpreferences 注入文件

shared_preference_injection_configuration.dart shared_preference_injection_configuration.dart

 import 'package:get_it/get_it.dart'; import 'package:injectable/injectable.dart'; import 'package:shared_preferences/shared_preferences.dart'; final GetIt getIt = GetIt.instance; @injectableInit Future<void> configureInjection(String env) async { await $initGetIt(getIt, environment: env); } Future<void> initDependencies() async { await _initSharedPref(); } Future<void> _initSharedPref() async { final SharedPreferences sharedPref = await SharedPreferences.getInstance(); getIt.registerSingleton<SharedPreferences>(sharedPref); }

shared_preference_injection.dart shared_preference_injection.dart

 import 'package:injectable/injectable.dart'; import 'package:shared_preferences/shared_preferences.dart'; @module abstract class InjectionModule { //injecting third party libraries @preResolve Future<SharedPreferences> get prefs => SharedPreferences.getInstance(); }

Below lies the main method.It already has the configureinjection and initdependencies method but still gives me the same error.下面是主要方法。它已经有 configureinjection 和 initdependencies 方法,但仍然给我同样的错误。

main.dart main.dart

 void main() async { // Make sure all bindings are loaded for easy localization WidgetsFlutterBinding.ensureInitialized(); await EasyLocalization.ensureInitialized(); await configureInjection(Environment.prod); await initDependencies(); // ignore: always_specify_types EasyLocalization.logger.enableBuildModes = []; // Load bloc Observer for debugging BlocOverrides.runZoned(() => SimpleBlocObserver()); // Define main breakpoints for responsive_builder ResponsiveSizingConfig.instance.setCustomBreakpoints( const ScreenBreakpoints(desktop: PDesktopSize, tablet: PTabletSize, watch: PWatchSize), ); // Set default locale to german style // Intl.defaultLocale = 'de_DE'; // Load bloc Observer for debugging runZonedGuarded( () async { await BlocOverrides.runZoned( // Main Entry Point for our application // Translations wrapping around the whole app and start the structure for Cecht () async => runApp( EasyLocalization( supportedLocales: const <Locale>[ Locale('en'), Locale('de'), ], path: 'assets/translations_v5', fallbackLocale: const Locale('de'), child: MyApp(), ), ), blocObserver: SimpleBlocObserver(), ); }, (Object error, StackTrace stackTrace) => Logger().logWarning('${error.toString()} $stackTrace'), ); }

Okay so when you're doing blocTesting then you need to register your packages again in the setup function in it which is in the case you are using getit for dependency injection.好的,所以当您进行 blocTesting 时,您需要在其中的设置 function 中再次注册您的包,这是在您使用 getit 进行依赖注入的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM