Building a Todo/CRUD Application In Flutter With Redux - Part 3

Repository

https://github.com/flutter/flutter

What Will I Learn?

  • You will learn about TypedMiddleware to create a Middleware chain
  • You will learn about TypedReducers to modularize your Reducers
  • You will learn how to further refactor a Flutter Redux application
  • You will learn how to use CombinedReducer to make a Monolithic Reducer
  • You will learn how to use ToString overrides to show state in the Redux Dev Tools

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A basic knowledge of Redux
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Basic knowledge of CRUD

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Outline and Overview

In this Flutter Redux Tutorial, we take a look at some of the more advanced concepts that are somewhat unique to the Flutter Redux and Dart Redux implementations. We look at how we can use the combinedReducers function and the TypedReducer types to create a modular and monolithic reducer out of multiple reducers. Also, we take a look at TypedMiddleware and how we can use it to chain a bunch of middleware functions together. Both of these methods allow us to further refactor the code to make it more readable and easy to add to. We also take a look as some other ideas such as the ability to see state inside of the Redux Dev tools by implementing a toString method for each of our data models.

Description

Making Modular and Readable Code with TypedMiddleware

From the outset, Redux middleware functions are entirely composable because they are pure functions which follow a the same function signature. In the Dart implementation of the Redux pattern, we are able to take advantage of this concept to split our middleware into small little functions; each which cover their own concern and action. The TypedMiddleware type allow us to organically bind these smaller functions to their respective actions so that the code can naturally find which function needs to be executed for which action. By doing this, we reduce the boilerplate for our applications and we make our application logic naturally more modular.

middle-ware-types.png

In the above image is the function which is used to create our list of middleware. We execute the side effects prior to returning the list of middleware and then used the TypedMiddleware type to bind the application state to these functions and the appropriate actions. In this way, it is very easy to read the code and edit it should we have to add more middleware to our application. By returning a middleware closure function from the execution of these smaller functions the code fully makes use of the composability of our middleware.

Combining Reducers into a Single Reducer

In keeping with this theme of modularity and composability, we can also apply the same ideas to our Reducers. However, in this case, rather then making a bunch of smaller functions which are executed one at a time and chained together through the next dispatcher; our reducers can be further composed by using the combinedReducers function. This function takes the small reducer functions and composes them into a large monolithic reducer which contains all of the logic that manipulates our application's state.

typed-reducer.png

In the image above, we use the TypedReducer type to bind a input type and an action to the smaller reducer functions that we built in this file. The list of TypedReducers is then fed into the combinedReducer function which is also annotated with the input type. Again, this cleans up the concerns of our code, increases modularity and expands our ability to quickly add new reducers to this larger function.

Viewing State in the Redux Dev Tools

While we did look at the Redux Dev Tools in the last tutorial, we didn't fully explore the features of this set of tools. Specifically, we were unable to directly look at our state because the tools did not know how to properly parse our data types into a readable format. This can be corrected by overriding the toString method which is attached to every class in Dart.

state.png

We take advantage of the already written toJson functions for both of our data types. Because it already creates a map which looks like JSON, we can easily just convert that map into a single string which can then be passed back from the toString method. The Redux Dev Tools can then properly read the state and display it in its widget's state field as seen in the above image.

The source code for this project can be found here

Video Tutorial

Related Videos

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Proof of Work Done

https://github.com/tensor-programming

H2
H3
H4
3 columns
2 columns
1 column
4 Comments