Flutter Provider vs Riverpod

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

by The Captain

on
June 14, 2023

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

State management is a crucial concept in Flutter development. While there are several state management frameworks to choose from, the two most popular ones are Flutter Provider and Riverpod. In this tutorial, we will compare these two frameworks based on their features, ease of use, and performance.

Flutter Provider

Flutter Provider is a simple and lightweight state management solution that uses InheritedWidgets to propagate changes down the widget tree. It follows the principles of dependency injection, making it easy to manage class instances and share data between widgets.

Here is an example of using Flutter Provider in a Flutter app:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider(create: (_) => Database()),
        ChangeNotifierProvider(create: (_) => Settings()),
      ],
      child: MaterialApp(
        title: 'MyApp',
        home: HomePage(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('MyApp')),
      body: Center(
        child: Consumer<Settings>(
          builder: (context, settings, _) {
            return Text(settings.themeName);
          },
        ),
      ),
    );
  }
}

In this example, we are using the MultiProvider widget to provide instances of the Database and Settings classes to the widget tree. The Consumer widget is used to get the current value of the Settings object and update the UI accordingly.

Riverpod

Riverpod is a newer framework that is somewhat similar to Flutter Provider. It uses provider functions to create and manage state, making it more flexible and powerful than Flutter Provider. It also uses an improved syntax that is easier to read and write.

Here is an example of using Riverpod in a Flutter app:

final databaseProvider = Provider((_) => Database());
final settingsProvider = ChangeNotifierProvider((_) => Settings());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ProviderScope(
      child: MaterialApp(
        title: 'MyApp',
        home: HomePage(),
      ),
    );
  }
}

class HomePage extends ConsumerWidget {
  @override
  Widget build(BuildContext context, ScopedReader watch) {
    final settings = watch(settingsProvider);
    return Scaffold(
      appBar: AppBar(title: Text('MyApp')),
      body: Center(
        child: Text(settings.themeName),
      ),
    );
  }
}

In this example, we are using provider functions to create and manage the Database and Settings objects. The ProviderScope widget is used to define the scope of the providers. The watch function is used to access the current value of the Settings object and update the UI.

Comparison

Both Flutter Provider and Riverpod are excellent state management solutions for Flutter apps. They are both easy to use and provide excellent performance. However, there are some differences between them that you should consider before choosing one.

  • Provider syntax: Riverpod uses a more streamlined syntax that makes it easier to understand and use.
  • Flexibility: Riverpod is more flexible than Flutter Provider, supporting more complex state management scenarios.
  • Scoping: Riverpod provides more advanced scoping features than Flutter Provider, making it easier to manage state between different parts of your app.

Conclusion

Both Flutter Provider and Riverpod are excellent state management solutions for Flutter apps. Which one you choose depends on your development needs and personal preferences. If you prefer a lightweight and simple solution, Flutter Provider is an excellent choice. If you want more flexibility and advanced features, Riverpod is the way to go.