Search code examples
flutterdependenciescode-injection

Flutter - Dependency Injection


I am after some best practise tips for developing my Flutter app.

Currently, I have an app with multiple pages and multiple plugins such as network connection, SQLite, Location etc.

Currently, on each page, I am creating a new instance of each plugin I need access to as shown below, and then using the plugin functionality.

final _secureStorage = FlutterSecureStorage();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

What I want to achieve: I would like to possibly only create an instance of these classes once, and then be able to access the instance in all pages - something like Dependency Injection.

Currently, I am looking into InheritedWidget widget or the Provider package, however, I am not sure if they do what I am trying to achieve as I don't want to inherit or pass around widgets, I want to inject classes instances.


Solution

  • You could try out, the get_it package, since it is not tied to Flutter.

    https://pub.dev/packages/get_it