Skip to content

Latest commit

 

History

History
108 lines (84 loc) · 3.86 KB

Provider-types.md

File metadata and controls

108 lines (84 loc) · 3.86 KB

Riverpod is a state management library for Flutter that provides a simple, scalable, and testable way to manage state. It builds on the ideas introduced by the Provider package but introduces significant improvements. Here are the main types of providers in Riverpod and their uses:

  1. Provider:

    • Purpose: Provides a value that is created once and never changes.
    • Use Case: Use for providing values like configurations, constants, or other dependencies that don’t need to change.
    final stringProvider = Provider<String>((ref) {
      return 'Hello, world';
    });
  2. StateProvider:

    • Purpose: Provides a mutable state that can change over time.
    • Use Case: Use for managing simple state, similar to using State in a StatefulWidget.
    final counterProvider = StateProvider<int>((ref) {
      return 0;
    });
  3. FutureProvider:

    • Purpose: Provides a value asynchronously, which will eventually be completed with a value or an error.
    • Use Case: Use for managing values that are obtained asynchronously, like data from a network request.
    final userProvider = FutureProvider<User>((ref) async {
      final userRepository = ref.watch(userRepositoryProvider);
      return await userRepository.getUser();
    });
  4. StreamProvider:

    • Purpose: Provides a value that can change over time and is obtained from a stream.
    • Use Case: Use for values that are provided as a stream, like real-time data or events.
    final messagesProvider = StreamProvider<List<Message>>((ref) {
      final chatService = ref.watch(chatServiceProvider);
      return chatService.getMessagesStream();
    });
  5. StateNotifierProvider (!!**CAUTION, USE NotifierProvider INSTEAD!!):

    • Purpose: Provides a state notifier that can handle more complex state logic.
    • Use Case: Use for more complex state management where you need to handle multiple states or have more logic to manage state transitions.
    class CounterNotifier extends StateNotifier<int> {
      CounterNotifier() : super(0);
    
      void increment() => state++;
    }
    
    final counterNotifierProvider = StateNotifierProvider<CounterNotifier, int>((ref) {
      return CounterNotifier();
    });
  6. ChangeNotifierProvider (!!POCHO, DO NOT USE!!):

    • Purpose: Provides a ChangeNotifier, which is an observable object for managing state.
    • Use Case: Use for integrating with existing ChangeNotifier-based state management logic.
    class MyChangeNotifier extends ChangeNotifier {
      int _count = 0;
    
      int get count => _count;
    
      void increment() {
        _count++;
        notifyListeners();
      }
    }
    
    final myChangeNotifierProvider = ChangeNotifierProvider<MyChangeNotifier>((ref) {
      return MyChangeNotifier();
    });
  7. (Async)NotifierProvider:

    • Purpose: Similar to StateNotifierProvider, but uses the new Notifier API which is a part of Riverpod v1.0+.
    • Use Case: Use for state management with Notifier, allowing more flexibility and type safety.
    class MyNotifier extends Notifier<int> {
      MyNotifier() : super(0);
    
      void increment() => state++;
    }
    
    final myNotifierProvider = NotifierProvider<MyNotifier, int>((ref) {
      return MyNotifier();
    });
  8. AutoDispose:

    • Purpose: Automatically disposes of the provider when it is no longer needed, freeing up resources.
    • Use Case: Use for short-lived providers or when you want to ensure resources are cleaned up automatically.
    final autoDisposeProvider = Provider.autoDispose<String>((ref) {
      return 'This provider will be disposed of when not used';
    });

These providers give you flexibility in managing state in Flutter applications, allowing you to choose the right tool for your specific state management needs.