Managing State in Flutter Pragmatically

Book description

Explore popular state management techniques in Flutter and implement them in real-world applications

Key Features

  • Get to grips with popular approaches for managing your Flutter application state
  • Think declaratively in order to decide on the most fitting approach for different applications
  • Learn to implement state management solutions by building a popular use case in the form of a shopping cart app

Book Description

Flutter is a cross-platform user interface (UI) toolkit that enables developers to create beautiful native applications for mobile, desktop, and the web with a single codebase. State management in Flutter is one of the most crucial and complex topics within Flutter, with a wide array of approaches available that can make it easy to get lost due to information overload.

Managing State in Flutter Pragmatically is a definitive guide to starting out with Flutter and learning about state management, helping developers with some experience of state management to choose the most appropriate solutions and techniques to use. The book takes a hands-on approach and begins by covering the basics of Flutter state management before exploring how to build and manipulate a shopping cart app using popular approaches such as BLoC/Cubit, Provider, MobX, and Riverpod. Throughout the book, you'll also learn how to adopt approaches from React such as Redux and all its types.

By the end of this Flutter book, you'll have gained a holistic view of all the state management approaches in Flutter, and learned which approach is the best solution for managing state in your app development journey.

What you will learn

  • Understand the core concepts of different state management techniques used in Flutter
  • Build optimal and performant applications in Flutter
  • Develop an understanding of which technique to apply in what sort of apps
  • Build the habit of writing clean state management code
  • Produce code with techniques from beginner to advanced level for different state management solutions
  • Use state management techniques to create robust and scalable apps in Flutter

Who this book is for

This book is for developers who have already started with their Flutter journey and are now looking to learn optimal state management approaches for app development. The book will also help less experienced Flutter engineers to find the best state management solution to fit their app, along with Flutter engineers who want to learn which state management approach should be taken under what circumstances.

Table of contents

  1. Managing State in Flutter Pragmatically
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Section 1:The Basics of State Management
  7. Chapter 1: States and State Management Overview
    1. Flutter – a brief introduction
    2. What is a state?
    3. Importance of a state in an application
      1. Different states in a large application
    4. What is state management?
    5. Choosing the best technique for your application
    6. Summary
  8. Chapter 2: The Core Building Blocks of State Management
    1. Technical requirements
    2. Managing states within a widget with setState
      1. The counter application example
      2. Widget rebuilding – widget trees
      3. Passing data to another screen – setState example
      4. Updating the first screen's counter value
      5. Section overview – setState
      6. Optional challenge – setState
    3. Inherited widgets – injecting the state at the root
      1. Inherited widgets – updating the counter variable
      2. Section overview – inherited widgets
    4. Inherited models – optimizing inherited widgets
      1. Remember the optional challenge?
      2. Section overview – inherited models
    5. Summary
  9. Section 2:Types, Techniques, and Approaches
  10. Chapter 3: Diving into Advanced State Management Approaches
    1. Technical requirements
    2. Provider – using less boilerplate code than InheritedWidget
      1. Adding a Provider dependency in a sample application
      2. Consuming Provider using the ChangeNotifierProvider class
      3. Section overview – Provider
    3. Riverpod – enhancing Provider
      1. Adding a Riverpod dependency in a sample application
      2. Using the simplest version of Riverpod to update the counter value
      3. Using a custom class as a state in Riverpod
      4. Section overview – Riverpod
    4. BLoC – writing business logic separately
      1. Adding a BLoC dependency in your application
      2. Creating a counter example application using BLoC
      3. Optional challenge – BLoC
      4. Section overview – BLoC
    5. Cubit – simplified BLoC
      1. Creating a counter example application using Cubit
      2. Section overview – Cubit
    6. Summary
  11. Chapter 4: Adopting State Management Approaches from React
    1. Technical requirements
    2. Redux – using unidirectional data flow
      1. Adding a Redux dependency to a sample app
      2. Consuming Redux using StoreProvider in code
      3. Section overview – Redux
    3. MobX – using observables with the fewest lines of code
      1. Adding MobX, Build Runner, and Codegen dependencies to the sample app
      2. Using code generation to create boilerplate code through build runner
      3. MobX – section overview
    4. Summary
  12. Chapter 5: Executing Distinctive Approaches Like GetX, GetIt, and Binder
    1. Technical requirements
    2. GetX – simplified reactions
      1. Adding a GetX dependency in a sample application
      2. Example of a counter application using GetBuilder
      3. Exploring more in GetX
      4. Section overview – GetX
      5. Optional challenge – GetX
    3. GetIt – no BuildContext required
      1. Adding a GetIt dependency in a sample application
      2. Counter example application using GetIt
      3. Section overview – GetIt
    4. Binder – using scopes to separate business logic
      1. Adding a Binder dependency in a sample application
      2. Counter example application using Binder
      3. Section overview – Binder
    5. Summary
  13. Section 3:Code-Level Implementation
  14. Chapter 6: Creating a Shopping Cart Application Using Basic Approaches
    1. Technical requirements
    2. Creating a shopping cart application with setState
      1. Section overview
      2. Optional challenge
    3. Creating a shopping cart application with InheritedWidget
      1. Section overview
      2. Optional challenge
    4. Creating a shopping cart application with InheritedModel
      1. Section overview
    5. Summary
  15. Chapter 7: Manipulating a Shopping Cart Application through BLoC, Provider, and React-Based Approaches
    1. Technical requirements
    2. Creating a shopping cart application with BLoC
    3. Creating a shopping cart application with Cubit
    4. Creating a shopping cart application with Provider
    5. Creating a shopping cart application with Riverpod
    6. Creating a shopping cart application with Redux
    7. Creating a shopping cart application with MobX
    8. Summary
  16. Chapter 8: Using GetX, GetIt, and Binder to Update the Cart Application
    1. Technical requirements
    2. Creating a cart app with GetX
      1. Optional challenge – GetX
    3. Creating a cart app with GetIt
    4. Creating a cart app using Binder
    5. Summary
  17. Chapter 9: Comparative State Management Analysis: When to Use What?
    1. Technical requirements
    2. Revisiting the techniques
      1. setState
      2. InheritedWidget and InheritedModel
      3. Provider
      4. Riverpod
      5. BLoC and Cubit
      6. Redux
      7. MobX
      8. GetX
      9. GetIt
      10. Binder
    3. Mapping different techniques to different application ideas
      1. setState
      2. InheritedWidget/InheritedModel
      3. Provider/Riverpod
      4. BLoC/Cubit
      5. Distinctive approaches
      6. Redux
      7. MobX
      8. Creating a good architecture
      9. Author's choice of state management technique – a hybrid approach
      10. A short overview of state management decision-making
    4. Summary
    5. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you

Product information

  • Title: Managing State in Flutter Pragmatically
  • Author(s): Waleed Arshad
  • Release date: November 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781801070775