Skip to content

fidelmak/basic_Riverpod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Learn Basic Riverpod for State Management

A new Flutter project.

Getting Started

๐ŸŒŠ Basic Riverpod for State Management in Flutter

Riverpod is a powerful and modern state management solution for Flutter. Itโ€™s type-safe, testable, and avoids many limitations of the old Provider package.


๐Ÿงฉ 1. Installation

Add Riverpod to your pubspec.yaml:

dependencies:
  flutter_riverpod: ^2.5.0

Then, import it:

import 'package:flutter_riverpod/flutter_riverpod.dart';

๐Ÿ—๏ธ 2. Wrap Your App with ProviderScope

Every Riverpod app needs a ProviderScope at the root โ€” it stores all providersโ€™ states.

void main() {
  runApp(
    ProviderScope(
      child: MyApp(),
    ),
  );
}

๐Ÿ”น 3. Understanding Providers

Providers are how you declare and share state across your app.


๐Ÿงฎ Example 1: Provider (Read-only value)

Used for constants or computed values.

final nameProvider = Provider<String>((ref) => "Paul Fidelis");

class HomeScreen extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final name = ref.watch(nameProvider);
    return Text('Hello, $name');
  }
}

๐ŸŸข ref.watch() listens to the provider ๐Ÿ”น Use when the value does not change


๐Ÿ”„ Example 2: StateProvider (Simple mutable state)

Used for simple values like counters, booleans, etc.

final counterProvider = StateProvider<int>((ref) => 0);

class CounterScreen extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final count = ref.watch(counterProvider);
    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: Center(
        child: Text('Count: $count', style: TextStyle(fontSize: 24)),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          ref.read(counterProvider.notifier).state++;
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

๐ŸŸข ref.watch() โ€” listens to state ๐ŸŸก ref.read() โ€” reads or modifies state without listening


โš™๏ธ Example 3: StateNotifierProvider (Complex state logic)

Used when you need custom logic (e.g., API, business rules, lists).

// 1๏ธโƒฃ Create a Notifier class
class CounterNotifier extends StateNotifier<int> {
  CounterNotifier() : super(0);

  void increment() => state++;
  void reset() => state = 0;
}

// 2๏ธโƒฃ Create Provider
final counterNotifierProvider =
    StateNotifierProvider<CounterNotifier, int>((ref) => CounterNotifier());

// 3๏ธโƒฃ Use in Widget
class CounterNotifierScreen extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final count = ref.watch(counterNotifierProvider);
    final notifier = ref.read(counterNotifierProvider.notifier);

    return Scaffold(
      appBar: AppBar(title: Text('Counter Notifier')),
      body: Center(child: Text('Count: $count', style: TextStyle(fontSize: 24))),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(onPressed: notifier.increment, child: Icon(Icons.add)),
          SizedBox(height: 10),
          FloatingActionButton(onPressed: notifier.reset, child: Icon(Icons.refresh)),
        ],
      ),
    );
  }
}

๐Ÿ’ก StateNotifierProvider = clean separation of UI and logic


๐ŸŒ Example 4: FutureProvider (Async data from API)

Used for loading async data (e.g., from network or database).

final userProvider = FutureProvider<String>((ref) async {
  await Future.delayed(Duration(seconds: 2));
  return "User: Paul";
});

class UserScreen extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final userAsync = ref.watch(userProvider);

    return userAsync.when(
      data: (user) => Center(child: Text(user)),
      loading: () => Center(child: CircularProgressIndicator()),
      error: (err, stack) => Center(child: Text('Error: $err')),
    );
  }
}

๐Ÿ” Example 5: StreamProvider (Realtime data)

Used for Firebase streams, sockets, etc.

final timeProvider = StreamProvider<DateTime>((ref) async* {
  while (true) {
    await Future.delayed(Duration(seconds: 1));
    yield DateTime.now();
  }
});

class ClockScreen extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final timeAsync = ref.watch(timeProvider);

    return timeAsync.when(
      data: (time) => Center(child: Text(time.toString())),
      loading: () => Center(child: CircularProgressIndicator()),
      error: (e, s) => Center(child: Text('Error: $e')),
    );
  }
}

๐Ÿง  6. When to Use Each Provider

Type Use Case Example
Provider Read-only value Theme, Config
StateProvider Simple counter or toggle Switch, counter
StateNotifierProvider Complex state + logic Todos, Auth
FutureProvider Async one-time fetch API call
StreamProvider Realtime updates Firebase, Clock

๐Ÿงพ 7. Tips

โœ… Use ConsumerWidget for simplicity โœ… Use ref.watch() inside build methods โœ… Use ref.read() in callbacks or event handlers โœ… Keep logic in StateNotifier, not the UI


๐ŸŽฏ Example Folder Structure

lib/
 โ”ฃ main.dart
 โ”ฃ providers/
 โ”ƒ โ”— counter_provider.dart
 โ”ฃ screens/
 โ”ƒ โ”— counter_screen.dart
 โ”— widgets/

๐Ÿงฉ Bonus: Refresher Summary

Concept Description
ProviderScope Root container for providers
WidgetRef Used to access providers
ref.watch() Listen and rebuild on change
ref.read() Read without listening
ref.refresh() Force reload a provider

Riverpod = Simple + Scalable + Testable You can start small (StateProvider) and grow to advanced (StateNotifierProvider, async providers) easily.


โœ๏ธ Next Step Ideas

  • Try replacing your setState() with StateProvider
  • Create a todo list with StateNotifierProvider
  • Fetch API data using FutureProvider

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors