如何解决 getit 共享首选项错误?
how to solve getit shared preferences 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?)
下面是我的 sharedpreferences 注入文件
shared_preference_injection_configuration.dart
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:mynovatium/app/helper/injections/shared_preference/shared_preference_injection_configuration.config.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
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();
}
下面是主要的 method.It 已经有了 configureinjection 和 initdependencies 方法,但仍然给我同样的错误。
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: kDesktopSize, tablet: kTabletSize, watch: kWatchSize),
);
// 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: MyNovatium(),
),
),
blocObserver: SimpleBlocObserver(),
);
},
(Object error, StackTrace stackTrace) => KBMLogger().logWarning('${error.toString()} $stackTrace'),
);
}
好的,所以当您进行 blocTesting 时,您需要在其中的设置函数中再次注册您的包,这是在您使用 getit 进行依赖项注入的情况下。
我正在尝试对我的项目进行 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?)
下面是我的 sharedpreferences 注入文件
shared_preference_injection_configuration.dart
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:mynovatium/app/helper/injections/shared_preference/shared_preference_injection_configuration.config.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
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();
}
下面是主要的 method.It 已经有了 configureinjection 和 initdependencies 方法,但仍然给我同样的错误。
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: kDesktopSize, tablet: kTabletSize, watch: kWatchSize),
);
// 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: MyNovatium(),
),
),
blocObserver: SimpleBlocObserver(),
);
},
(Object error, StackTrace stackTrace) => KBMLogger().logWarning('${error.toString()} $stackTrace'),
);
}
好的,所以当您进行 blocTesting 时,您需要在其中的设置函数中再次注册您的包,这是在您使用 getit 进行依赖项注入的情况下。