类型为NavigationService的对象/工厂未在GetIt内注册



这个错误一直出现在我的开发中

调试控制台

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: 'package:get_it/get_it_impl.dart': Failed assertion: line 312 pos 7: 'instanceFactory != null': Object/factory with  type NavigationService is not registered inside GetIt.
E/flutter (27190): (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
E/flutter (27190): Did you forget to register it?)

这是导航服务文件,其中初始化了导航服务和getit

navigation_service.dart

GetIt locator = GetIt.instance;
setupLocator() {
locator.registerLazySingleton(() => NavigationService());
}
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
Future<dynamic> navigateTo(routeName) {
return navigatorKey.currentState.pushReplacementNamed(routeName);
}
goBack() {
return navigatorKey.currentState.pop();
}
}

dynamic_link_service.dart

.
.
.
#some code
final NavigationService _navigationService = locator<NavigationService>();
if (deepLink != null) {
// print('_handleDeepLink | deepLink: $deepLink');
UserDataProvider().setReferralData(deepLink);
_navigationService.navigateTo(ReferralSignupPage.routeName);
}
.
.
.

main.dart

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await setupLocator();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home:
.
.
.
routes: {
ReferralSignupPage.routeName: (ctx) => ReferralSignupPage(),
},
navigatorKey: locator<NavigationService>().navigatorKey,
);
}

即使在尝试了其他问题中提供的所有可能的解决方案后,这个错误仍然会出现请帮我摆脱困境。

问候

这里只是猜测,但不是语法:

final service = locator.get<NavigationService>();

注册服务时也可能需要提供通用参数。

添加setupLocator((;

void main() {
setupLocator();
runApp(MyApp());
}

最新更新