I have this global bindings class to initialise some services and I need it to be initialised straight away:
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);
}
}
Which is in my initialBindings:
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,
);
}
}
Then in a class constructor I try to "find" it:
class UserLocalRepository extends VpService implements IUserLocalRepository {
UserLocalRepository() {
localDataSource = Get.find<LocalDataSource>();
}
LocalDataSource localDataSource;
And I get this error:
══════ 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
...
════════════════════════════════════════════════════════════════════════════════
This is the binding mentioned in the error message:
class LoggedOutNickNameBinding extends Bindings {
LoggedOutNickNameBinding() {
_repository = Get.put(UserLocalRepository());
}
IUserLocalRepository _repository;
@override
void dependencies() {
Get.lazyPut<LoggedOutNickNameController>(
() => LoggedOutNickNameController(_repository),
);
}
}
Why are the "initialBindings" not initialised, so that my app can "find" them when the app starts up?
I'm guessing that there's a timing / ordering mismatch with when your GlobalBindings.dependencies() method gets called and when you need those resources.
You could try initializing your Bindings class prior to GetMaterialApp instead of passing your Bindings class to GetMaterialApp.
Tangent
Just guessing here, but are some of the classes you're initializing via Get.put are slow-startup (i.e. async) before they are ready to use?
If so you could use
Example
I recently ran an exercise of performing async Bindings initialization prior to app being loaded for user interaction. Here's the code: