'Flutter objectbox initialization
How can i initialize properly the instance of my DatabaseProvider? I'm struggling with it. I found solutions but everyone of them initialize objectbox in the widgets, but i would use Bloc pattern in my application and would use objectbox in my repositories.
Here is the DatabaseProvider:
import 'package:testroject/data/models/user.dart';
import 'package:testproject/objectbox.g.dart';
import 'package:objectbox/objectbox.dart';
import '../../util/logging.dart';
class DatabaseProvider {
late Store? store;
DatabaseProvider() {
create();
}
DatabaseProvider._create(this.store) {}
static Future<DatabaseProvider> create() async {
final store = await openStore();
return DatabaseProvider._create(store);
}
Future<void> updateUser(User user) async {
store?.box<User>().put(user);
}
User? getUser() {
User? user;
try {
user = store?.box<User>().getAll().first;
} catch (e) {
logger.i(e);
}
return user;
}
}
The Store is always null.
I would use this DatabaseProvider in the repositories like:
class UserRepository implements IUserRepository {
final database = ServiceLocator.getIt<DatabaseProvider>();
Provide the instance of the DatabaseProvider in the main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
ApplicationInitialization().initializeRepositories();
ServiceLocator.getIt.registerSingleton<DatabaseProvider>(DatabaseProvider());
runApp(AppMain(appRouter: ApplicationRouter()));
}
So i would inject in the repositories. Is there anyone who can help me with this? Thank all of you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
