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

in #utopian-io6 years ago (edited)

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

Sort:  

Fine work @tensor. Learners are getting thorough instruction on this. I like how you provide tips and tricks throughout as part of the instruction. "You can think of this as..." "This will help you clean up ...." Very nice personal instruction.

Bucky

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @buckydurddle!

So far this week you've reviewed 3 contributions. Keep up the good work!

Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @tensor!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

You have a minor misspelling in the following sentence:

You will learn how to futher refactor a Flutter Redux application.
It should be further instead of futher.

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.034
BTC 66396.53
ETH 3174.43
USDT 1.00
SBD 4.15