i want to access cameras
in another dart file, how can i do it, my app is complex can’t be shared through constructor, i am using Getx (i am new to getx), help is appriciated
this is my main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final cameras = await availableCameras();
runApp(MyApp());
}
2
Answers
It’s a local variable, but you can declare it globally and make a getter function for it:
You can call
getCameras()
orcameras
itself anywhere to get the value of that variable.In this case, using a service locator seems to be a proper solution. A good candidate would be
get_it
.Setup:
After installing it in your project:
You could declare a global instance of it:
The recommendation for registering classes is to have an abstraction layer. Example:
Note that you can also register concrete types. The recommendation for using the abstract base classes as a registration type is to vary the implementations.
Therefore, you could register it as:
Make sure to register your class(s) when launching – in the main before calling
runApp(const MyApp());
Usage:
Now, you have access to it throughout your whole app’s scope!
You might want to look at the different methods for registering for your class;
registerSingleton
should be suitable here…