Flutter Redux

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

by The Captain

on
May 14, 2023

Flutter Redux: A Predictable State Management Framework

Flutter Redux is a state management framework for Flutter that is based on the popular Redux library. It helps you manage the state of your Flutter app in a predictable and organized way, making it easier to build and maintain complex applications. In this tutorial, we will cover the basics of Flutter Redux and how to implement it in your app.

Getting Started with Flutter Redux

To get started with Flutter Redux, you need to add the following dependencies to your Flutter project in `pubspec.yaml` file:
dependencies:
  flutter_redux: ^0.8.2
  redux: ^5.0.0}
Once you have added the dependencies, you can create a Redux store in your app by using the `createStore()` method from the `redux` package. Here's an example of how to create a store:
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';

enum Actions { Increment }

int counterReducer(int state, dynamic action) {
  if (action == Actions.Increment) {
    return state + 1;
  }
  return state;
}

void main() {
  final store = Store(counterReducer, initialState: 0);
}
In the above code, we created a `counterReducer` function that takes the current state and an action as input and returns a new state based on the action type. In this case, we have a single `Increment` action that increments the state by 1. The `main()` function creates a new store using the `counterReducer` and initializes its state to 0.

Connecting Flutter Redux to Your App

To connect Flutter Redux to your app, you need to wrap the root widget of your app with a `StoreProvider` widget from the `flutter_redux` package:
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';

void main() {
  final store = Store(counterReducer, initialState: 0);

  runApp(MyApp(
    store: store,
  ));
}

class MyApp extends StatelessWidget {
  final Store store;

  MyApp({Key key, this.store}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StoreProvider(
      store: store,
      child: MaterialApp(
        title: 'Flutter Redux Demo',
        home: MyHomePage(),
      ),
    );
  }
}
In the above code, we wrapped the `MyApp` widget with a `StoreProvider` widget and passed in the `store` that we created in the previous step. To access the state of the store in any of your widgets, you can use the `StoreConnector` widget from the `flutter_redux` package:
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Redux Demo'),
      ),
      body: Center(
        child: StoreConnector(
          converter: (store) => store.state,
          builder: (context, counter) {
            return Text(
              'Counter: $counter',
              style: Theme.of(context).textTheme.headline4,
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          StoreProvider.of(context).dispatch(Actions.Increment);
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
In the above code, we used the `StoreConnector` widget to access the state of the store by using the `converter` function to convert the store's state to the `counter` variable. We then used the `builder` function to build the widget using the `counter` variable. The `floatingActionButton` is used to dispatch the `Increment` action to the store when it is pressed.

Conclusion

Flutter Redux is a powerful state management framework for Flutter that helps you manage the state of your app in a predictable and organized way. In this tutorial, we covered the basics of Flutter Redux and how to implement it in your app.