我有这个全局绑定类来初始化一些服务,我需要立即初始化它:
import 'package:get/get.dart';
import 'package:vepo/data/data_provider/local_data_provider.dart';
import 'package:vepo/data/data_source/local_data_source.dart';
import 'services/authentication_service.dart';
class GlobalBindings extends Bindings {
final LocalDataProvider _localDataProvider = LocalDataProvider();
@override
void dependencies() {
Get.put<AuthenticationService>(AuthenticationService(), permanent: true);
Get.put<LocalDataProvider>(_localDataProvider, permanent: true);
Get.put<LocalDataSource>(LocalDataSource(_localDataProvider),
permanent: true);
}
}
在我的初始绑定中:
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Vepo',
initialRoute: AppPages.INITIAL,
initialBinding: GlobalBindings(),
transitionDuration: const Duration(milliseconds: 500),
defaultTransition: Transition.rightToLeft,
getPages: AppPages.routes,
home: Root(),
theme: homeTheme,
);
}
}
然后在类构造函数中我尝试“找到”它:
class UserLocalRepository extends VpService implements IUserLocalRepository {
UserLocalRepository() {
localDataSource = Get.find<LocalDataSource>();
}
LocalDataSource localDataSource;
并得到这个错误:
══════ Exception caught by widgets library ═══════════════════════════════════
The following message was thrown building App(dirty):
"LocalDataSource" not found. You need to call "Get.put(LocalDataSource())" or "Get.lazyPut(()=>LocalDataSource())"
The relevant error-causing widget was
App
lib/main.dart:17
When the exception was thrown, this was the stack
#0 GetInstance.find
package:get/…/src/get_instance.dart:272
#1 Inst.find
package:get/…/src/extension_instance.dart:66
#2 new UserLocalRepository
package:vepo/…/user/user_local_repository.dart:10
#3 new LoggedOutNickNameBinding
package:vepo/…/logged_out_nickname/logged_out_nick_name_binding.dart:11
#4 AppPages.routes
package:vepo/…/routes/app_pages.dart:29
...
════════════════════════════════════════════════════════════════════════════════
这是错误消息中提到的绑定:
class LoggedOutNickNameBinding extends Bindings {
LoggedOutNickNameBinding() {
_repository = Get.put(UserLocalRepository());
}
IUserLocalRepository _repository;
@override
void dependencies() {
Get.lazyPut<LoggedOutNickNameController>(
() => LoggedOutNickNameController(_repository),
);
}
}
为什么“initialBindings”没有初始化,以便我的应用程序可以在应用程序启动时“找到”它们?