Flutter Provider vs Riverpod: Which is Better for State Management in Flutter?

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
June 6, 2023

Flutter Provider vs Riverpod: Which is Better for State Management in Flutter?

Flutter offers developers a wide range of frameworks for state management. Two popular state management frameworks are Provider and Riverpod. They both have similar architectures and use the same basic approach to state management. However, they differ in a few key areas. In this tutorial, we will explore those differences and help you decide which is better for your Flutter app.

Provider: A Quick Overview

Provider is a simple, easy-to-use framework for state management that is built on top of InheritedWidget. In a Provider architecture, Widgets above the provider request for data, while Widgets below the Provider rebuild themselves when that data has changed. It is powerful and flexible, and it allows you to manage almost any kind of data your app requires. Here's an example of a Counter App that uses Provider for state management.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_demo/counter.dart';

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => Counter(),
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final counter = Provider.of(context);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Provider Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('You have pushed the button this many times:'),
              Text(
                '${counter.count}',
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => counter.increment(),
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

Riverpod: A Lightweight Flutter State Management Framework

Riverpod is another state management framework for Flutter that is built on top of Provider. It is designed to be more flexible and easier to use than its parent framework while providing all the features you need for a complex app. Here's an example of a Counter App that uses Riverpod for state management.
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_demo/counter.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Riverpod Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Consumer(
                builder: (context, watch, child) {
                  final counter = watch(counterProvider);
                  return Text(
                    '${counter.count}',
                    style: Theme.of(context).textTheme.headline4,
                  );
                },
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => context.read(counterProvider).increment(),
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

final counterProvider = ChangeNotifierProvider((_) => Counter());}

Which is Better for State Management in Flutter?

In general, Provider is a good choice for simple apps while Riverpod is better suited for medium to complex apps. The main reason for this is the difference in how they are initialized. Riverpod allows for easier initialization of providers and more flexible dependency injection. Postsubject=State Management