0

i'm building a flutter App using navigation 2.0 and i'm starting to use ObjectBox library to store user appdata.

for now i initialize it that way in my main.dart then import the variable objectBox whenever i want to use it in the viewModels.

late ObjectBox objectBox;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
objectBox = await ObjectBox.init();
runApp(const MyApp());
}

i initialise it like that and put objectBox as a global variable ... but i think it's not good at all. what are the good practices around that?

MyApp is basically

MaterialApp.router(
  title: 'Flutter Demo',
  theme: ThemeData(
    primarySwatch: Colors.blue,
  ),
  routeInformationParser: NavigationParser(),
  routerDelegate: NavigationDelegate(),
)

with the router ( NavigationDelegate() ) where i get to normally store global variables and initialise things i need in the app.

what would you advice me?

thanks, Badr

Badr Erraji
  • 141
  • 1
  • 11
  • i'm creating a singleton that i will create in the main and initialise it in the main. so that each time i call it, i already have acess to this _objectBox. what do you think of that? – Badr Erraji Sep 29 '22 at 10:47

1 Answers1

0

I think your approach is completely fine. Putting it as a global variable is a straightforward way to make sure you have access to ObjectBox through out your entire application. ObjectBox examples also used the same pattern.