· Flutter Deep Dives · 3 min read

Flutter dependency injection state management

When developing Flutter applications, managing the state of the app becomes crucial for a smooth user experience. One way to simplify state management is by utilizing dependency injection.

What is Dependency Injection?

Dependency injection is a design pattern that helps decouple components in an application by injecting dependencies from external sources rather than creating them within the component itself.

In Flutter, popular dependency injection libraries like get_it and provider provide convenient ways to manage dependencies and state.

Using Get_it for Dependency Injection

Get_it is a robust and easy-to-use dependency injection library for Flutter. Here’s an example of how to set up Get_it for state management:

  1. Add the get_it package to your pubspec.yaml file:
dependencies:
  get_it: ^7.0.0
  1. Create a service class that holds the state. For example, let’s create a simple counter service:
class CounterService {
  int counter = 0;

  void increment() {
    counter++;
  }
}
  1. Register the service within Get_it:
import 'package:get_it/get_it.dart';

GetIt locator = GetIt.instance;

void setupLocator() {
  locator.registerSingleton<CounterService>(CounterService());
}
  1. Initialize Get_it in your application’s entry point:
void main() {
  setupLocator();
  runApp(MyApp());
}
  1. Access the service from any Flutter widget:
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';

class CounterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final counterService = GetIt.instance<CounterService>();

    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Counter: ${counterService.counter}'),
            ElevatedButton(
              onPressed: () {
                counterService.increment();
              },
              child: Text('Increment'),
            ),
          ],
        ),
      ),
    );
  }
}

In this example, we can access the CounterService using GetIt.instance<CounterService>() within any widget that needs access to the counter state.

Using Provider for Dependency Injection

Provider is another popular state management library built on top of InheritedWidget and BuildContext. Here’s how you can use Provider for dependency injection:

  1. Add the provider package to your pubspec.yaml file:
dependencies:
  provider: ^6.0.0
  1. Create a service class similar to the previous example:
class CounterService extends ChangeNotifier {
  int _counter = 0;

  int get counter => _counter;

  void increment() {
    _counter++;
    notifyListeners();
  }
}
  1. Wrap your top-level widget using a ChangeNotifierProvider:
void main() {
  runApp(
    ChangeNotifierProvider<CounterService>(
      create: (_) => CounterService(),
      child: MyApp(),
    ),
  );
}
  1. Access the service from any Flutter widget using the Provider widget:
class CounterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final counterService = Provider.of<CounterService>(context);

    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Counter: ${counterService.counter}'),
            ElevatedButton(
              onPressed: () {
                counterService.increment();
              },
              child: Text('Increment'),
            ),
          ],
        ),
      ),
    );
  }
}

Here, the Provider.of<CounterService>(context) statement gives us access to the CounterService instance within the widget tree.

Conclusion

Using dependency injection for state management in Flutter can greatly simplify the management and sharing of state in your application. Whether you choose Get_it or Provider, both libraries offer convenient solutions to handle dependencies with ease.

#flutter #dependencyinjection

Share:
Back to Blog