Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Implement Fish-Redux State Management In Flutter

In this article, we will explore the Implement Fish-Redux State Management In Flutter. We perceive how to execute a demo program. We will show you how to work in your Flutter applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table of Contents :

Introduction — State Management in Flutter

Introduction — Fish-Redux state management

Key-concepts in Fish-Redux

How to use

Key Benefits

Final Output

GitHub Link

Reference Url

Conclusion


Introduction — State management:-

State management in Flutter refers to the techniques and tools used to manage and control the state (data and UI) of your Flutter application. Flutter applications often consist of complex UIs and dynamic data that can change over time.

Effective state management is crucial for creating responsive and maintainable apps. There are various approaches to state management in Flutter, and the choice of approach depends on the complexity of your application and your specific needs.

Introduction — Fish-Redux:-

Fish-Redux is a state management framework for building Flutter applications. It’s designed to help Flutter developers structure their applications in a way that makes code organization and maintenance easier, particularly for larger and more complex apps. Fish-Redux draws inspiration from concepts like Redux, a state management pattern used in web development, and applies them to the world of Flutter.

Key Concepts in Fish-Redux:-

  • State: In Fish-Redux, your application’s state is represented by plain Dart objects. These objects are typically immutable and describe the data that your application needs to function. State management in Fish-Redux revolves around creating, modifying, and sharing these state objects.
  • Action: Actions are the events or user interactions that trigger changes in your application’s state. Actions are dispatched to update the state. They carry information about what needs to change in the state.
  • Reducer: Reducers are responsible for taking the current state and an action and producing a new state. Reducers are pure functions that ensure the predictability and maintainability of the state management process.
  • Effect: Effects are side effects that can be triggered as a result of specific actions. They can be used for operations like making network requests, database access, or other asynchronous tasks. Fish-Redux provides a clean way to handle side effects.
  • Component: A component is a self-contained, reusable piece of the user interface. Each component in Fish-Redux consists of three parts: View, State, and Reducer. Components can be nested to build complex UI structures.
  • Page: A page in Fish-Redux is a logical collection of components. Pages help organize your app into meaningful sections. Each page has its state and can contain multiple components.

How to Use:-

Here are the basic steps to use Fish-Redux in your Flutter application:

  • Add Fish-Redux Dependency: Start by adding the Fish-Redux package as a dependency in your pubspec.yaml file:
dependencies:
fish_redux: ^0.3.7 # Use the latest compatible version
  • Define the Application State: Create a Dart class that represents the state of your application. This class should extend Cloneable. Define the properties and initial values for your application’s state.
Example:
import 'package:fish_redux/fish_redux.dart';

class CounterState implements Cloneable<CounterState> {
int count;
@override
CounterState clone() {
return CounterState()..count = count;
}
}
  • Define Actions: Create action classes that describe the events or user interactions that can change the state. Actions should include all the necessary data for the change.
Example:
import 'package:fish_redux/fish_redux.dart';

enum CounterAction { increment, decrement }
class CounterActionCreator {
static Action increment() {
return const Action(CounterAction.increment);
}
static Action decrement() {
return const Action(CounterAction.decrement);
}
}
  • Create Reducers: Write reducer functions that take the current state and an action as input and produce a new state as output. Reducers should be pure functions.
Example:
import 'action.dart';
import 'state.dart';

CounterState reducer(CounterState state, Action action) {
final CounterState newState = state.clone();
if (action.type == CounterAction.increment) {
newState.count += 1;
} else if (action.type == CounterAction.decrement) {
newState.count -= 1;
}
return newState;
}
  • Build Components: Create individual components for your UI. Each component includes a View (widget), State (describing the component’s local state), and Reducer (defining how to update the local state).
Example:
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'state.dart';
Widget buildView(CounterState state, Dispatch dispatch, ViewService viewService) {
return Scaffold(
appBar: AppBar(
title: Text('Fish Redux Counter Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Counter Value:',
style: TextStyle(fontSize: 20),
),
Text(
'${state.count}',
style: TextStyle(fontSize: 36, color: Colors.blue),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
IconButton(
icon: Icon(Icons.remove),
onPressed: () => dispatch(CounterActionCreator.decrement()),
),
IconButton(
icon: Icon(Icons.add),
onPressed: () => dispatch(CounterActionCreator.increment()),
),
],
),
],
),
),
);
}
  • Define Pages: Organize your components into pages. Each page has its state and can contain multiple components.
  • Initialize the Fish-Redux Store: Create a Store that holds the global application state, reducers, and middleware. This is the central hub for state management.
Example:
import 'package:fish_redux/fish_redux.dart';

import 'reducer.dart';
import 'state.dart';
Store<CounterState> createStore() {
return Store<CounterState>(
initialState: CounterState()..count = 0,
reducer: counterReducer,
);
}
  • Dispatch Actions: To update the state, dispatch actions to the store. The store will invoke the reducers to calculate the new state.
  • Build the UI: Use the components and pages to build the user interface. Components are generally built by buildView methods.
  • Handle Effects: For side effects like making API requests or accessing databases, use Effects to encapsulate the logic.

Fish-Redux provides a structured and organized way to manage the state of your Flutter application, making it easier to build and maintain large and complex apps.

Key Benefits:-

  • Centralized and Observable Data Management: Fish Redux simplifies data management by centralizing it through Redux. This means it retains all the advantages of Redux, and the framework even assembles the reducer automatically, making Redux usage more straightforward.
  • Component Division Management: Fish Redux divides views and data into components. By breaking down complex pages and data into smaller, independent modules, collaborative development within teams becomes much easier.
  • Isolation Between View, Effect, and Reducer: Each component is divided into three stateless and independent functions: View, Effect, and Reducer. Their statelessness makes them easy to write, debug, test, and maintain. This also allows for more flexibility in combining, reusing, and innovating.
  • Declarative Configuration Assemblies: Components and adapters are put together using free and declarative configuration, which includes defining a component’s view, reducer, effect, and its relationships with dependent child components.
  • Strong Scalability: The core framework focuses on its core responsibilities while providing flexibility for upper layers. While the framework itself doesn’t contain printed code, it allows observation of data flows and component changes through standard middleware. Additionally, mixins can be added to the component and adapter layers using Dart code, enhancing customizability and capabilities at the upper layer. The framework seamlessly communicates with other middlewares, such as those for automatic exposure and high availability, and allows for free assembly by the upper layer.
  • Small, Simple, and Complete: Fish Redux is incredibly lightweight, consisting of only around 1,000 lines of code. It’s user-friendly, requiring just a few small functions to set up before running. Despite its simplicity, Fish Redux provides a wide range of functionalities.

There’s a simple demo app below using fish-redux state management. Check the GitHub repo in the GitHub Link section.

Output:-

After running the demo project, we get

GitHub Link:-

GitHub – flutter-devs/fish-redux
Contribute to flutter-devs/fish-redux development by creating an account on GitHub.github.com


Reference Url:-

Flutter Analysis and Practice: App Framework Design Practices
This article describes the features and usage of Fish Redux for Xianyu’s application.alibaba-cloud.medium.com


Conclusion:-

This blog has provided a comprehensive understanding of Fish-Redux state management in Flutter applications. Now, you have the knowledge and tools to apply this powerful state management solution to your projects and explore the wide range of possibilities it offers. Enjoy your journey of exploration and development!

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Leave comment

Your email address will not be published. Required fields are marked with *.