Flutter Design Patterns and Best Practices

Book description

Elevate your mobile app development skills using reusable software development methodologies and code design principles, leveraging proven strategies from industry experts who have fostered thriving developer communities

Key Features

  • Cover code structure, app architecture, testing, performance optimization, error handling, and more
  • Get hands on with code examples and exercises to apply design patterns and best practices in real-world scenarios
  • Leverage the intricacies of the Flutter framework to build better apps for your clients
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

Flutter’s rapid adoption by developers and businesses alike has led to an increased demand for skilled developers who can deliver high-quality Flutter apps. Flutter can be a great tool to develop apps for yourself, but you don’t always need to consider things like scalability or business goals as a hobbyist. When you build apps for a business, however, you must use specific tools to help the business achieve its goals. This requires solutions to be fast, reliable, and delivered on time. This book will help you meet these business requirements.

You'll begin by learning how to build responsive UIs that work on all devices. Next, you'll delve into state management, understanding how to select the appropriate solution and gaining hands-on experience with InheritedWidget, ChangeNotifier, and BLoC. Later, you'll move on to high-level practices such as layered architecture, dependency injection, and repository patterns. The book will also show you how to develop features of any complexity, such as multithreading and native layer implementations. You'll also learn how to ensure the quality of your solutions by prioritizing testing.

By the end of this book, you'll be able to deliver well-architected Flutter projects that are stable, scalable, and maintainable.

What you will learn

  • Translate business requirements into technical solutions
  • Understand when and where specific design patterns would be applicable
  • Separate concerns into multiple layers with scoped responsibility
  • Build reusable architecture that can be applied to any type of app and supported by various teams
  • Debug and solve problems before they reach the user
  • Prevent production bugs by prioritizing testing during the development cycle
  • Design encapsulated features that are resilient to business requirement changes and software updates

Who this book is for

If you’re a mobile developer who has already realized the potential of Flutter as a tool to solve business problems and are looking forward to leveling up your app-building skills, then this book is for you. Freelancers, agencies, or individuals who have already developed basic applications with Flutter and want to level up their skills to build production-ready apps at scale will find this book helpful. Basic Flutter knowledge and experience in building apps will be beneficial. Moreover, if you’re transitioning from another mobile framework, this book will make the transition easier.

Table of contents

  1. Flutter Design Patterns and Best Practices
  2. Contributors
  3. About the authors
  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. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Building Delightful User Interfaces
  7. Chapter 1: Best Practices for Building UIs with Flutter
    1. Understanding the difference between declarative and imperative UI design
      1. Understanding the imperative paradigm
      2. Understanding the declarative paradigm
      3. Does Flutter use the declarative or imperative paradigm?
    2. Everything is a widget! Or is it?
      1. What is a widget?
      2. Getting to know the RenderObjectWidget and its children
      3. Unveiling the Element class
    3. Reduce, reuse, recycle!
      1. Pushing rebuilds down the tree
      2. Avoiding redundant rebuilds
      3. Avoiding redundant repaints
      4. Optimizing scroll view performance
    4. Summary
  8. Chapter 2: Responsive UIs for All Devices
    1. Technical requirements
    2. Understanding the Flutter layout algorithm
      1. Understanding BoxConstraints
      2. How do constraints determine the child widget’s size?
      3. Understanding the limitations of the layout rule
    3. Designing responsive apps with Flutter
      1. Getting to know the user’s device with MediaQuery
      2. Creating adaptive layouts
      3. Positioning widgets relative to each other
      4. Building flexible layouts
      5. Scrollable items
      6. Other layouts
    4. Ensuring accessibility in Flutter apps
      1. Getting to know accessibility widgets in Flutter
      2. Font size and color contrast
      3. Dev tooling for accessibility
      4. Manual review
    5. Summary
  9. Part 2: Connecting UI with Business Logic
  10. Chapter 3: Vanilla State Management
    1. Technical requirements
      1. What is state?
      2. Understanding the difference between ephemeral and app state
    2. Getting to know our Candy Store app
    3. Managing state the vanilla Flutter way
    4. Lifting the state up
      1. Understanding the Observer pattern
    5. Passing around dependencies via InheritedWidget
      1. What is InheritedWidget?
      2. Understanding the .of(context) pattern
      3. Creating the CartProvider class
    6. Interacting with BuildContext in the right place, at the right time
      1. What is BuildContext?
      2. Context does not contain the widget
      3. Context contains the widget, but not the one you expect
      4. Context accessed too early!
      5. Context accessed too late!
    7. Summary
  11. Chapter 4: State Management Patterns and Their Implementations
    1. Technical requirements
    2. Diving into the world of MVX patterns
    3. Defining criteria for the state management pattern
    4. Embracing data binding with MVVM in Flutter
      1. Extracting data logic into the Model
      2. Emitting data via the Streams API
      3. Encapsulating state in CartState
    5. Implementing the MVVM and MVI patterns with flutter_bloc
      1. What is a cubit?
      2. Understanding the MVI pattern
      3. Implementing MVI with BLoC
      4. Embracing the UDF pattern
    6. Implementing the Segmented State Pattern
    7. Avoiding bugs related to state equality and mutability
    8. Summary
  12. Chapter 5: Creating Consistent Navigation
    1. Technical requirements
    2. Getting started with navigation basics in Flutter
      1. Understanding Navigator 1.0 and routes
    3. Leveling up with advanced navigation techniques
      1. Navigator 2.0 and declarative routing
    4. Comparing Navigation 1.0 and 2.0
    5. Summary
  13. Part 3: Exploring Practical Design Patterns and Architecture Best Practices
  14. Chapter 6: The Responsible Repository Pattern
    1. Technical requirements
    2. Introducing the repository pattern
      1. How the repository pattern works
    3. Setting up our first repository
      1. Defining responsibilities
      2. Creating our first repository
    4. Defining repository interfaces
      1. Why use repository interfaces?
      2. Implementing repository interfaces
    5. Building a data source
      1. Repository interfaces and data sources
      2. Setting up remote data sources
      3. Integrating NetworkProductRepository
    6. Creating a local database using Hive
      1. Adding Hive to your project
      2. Creating a local data source
      3. Repository interfaces
    7. Implementing repository data retrieval
      1. Enhancing our product repository
      2. Integrating the repository
    8. Integrating the repository with our business logic
    9. Caching strategies
      1. Caching strategy
    10. Implementing offline mode
    11. Understanding data synchronization
      1. Implementing data synchronization
      2. Automating synchronization
      3. Refactoring the CartModel
    12. Summary
  15. Chapter 7: Implementing the Inversion of Control Principle
    1. Technical requirements
    2. Decoupling dependency creation from usage
      1. Identifying the singleton pattern
      2. Introducing the IoC principle
    3. Implementing the DI pattern via RepositoryProvider
      1. Injecting dependencies via a constructor
      2. Providing dependencies via RepositoryProvider
    4. Implementing SL pattern via get_it
      1. Bonus tip – using injectable
      2. Selecting the right tool for the job
    5. Summary
  16. Chapter 8: Ensuring Scalability and Maintainability with Layered Architecture
    1. Technical requirements
    2. Exploring layered architecture
      1. Introducing multitier architecture layers
      2. Implementing multitier architecture in the Candy Store app
    3. Defining layers and features
      1. Implementing layer-first architecture
      2. Implementing feature-first architecture
      3. Exploring file structure organization in Flutter
      4. Scoping dependencies to a feature life cycle
      5. How to connect multiple data sources
    4. Following software design principles
      1. Deciphering the acronyms of SOLID, DRY, KISS, and YAGNI
    5. Summary
  17. Chapter 9: Mastering Concurrent Programming in Dart
    1. Technical requirements
    2. Dart is single-threaded. Or is it?
      1. Understanding synchronous, concurrent, and parallel operations
      2. What is the event loop?
      3. Understanding blocking operations in Flutter
    3. Working with Future APIs in Dart
      1. What does the Future hold?
      2. Understanding the concept of async
      3. To chain Futures or to await, that is the question
      4. Handling independent Futures efficiently
    4. Embracing parallelism with isolates
      1. Implementing fuzzy search in the Candy Store app
      2. Understanding the concept of isolates
    5. Summary
  18. Chapter 10: A Bridge to the Native Side of Development
    1. Technical requirements
    2. Understanding Flutter app architecture
      1. Diving into the Flutter framework layers
      2. How does Flutter communicate with the native layer?
      3. Encoding data with MessageCodec
    3. Working with platform channels
      1. Selecting the platform channel type
      2. Implementing the UI of the Favorites feature
      3. Using MethodChannel for favoriting items
      4. Understanding Kotlin
      5. Understanding Swift
      6. Overviewing the problems introduced by vanilla MethodChannel channels
    4. Ensuring type-safe communication via pigeon code generation
      1. Configuring a pigeon interface
      2. Connecting client and host via pigeon-generated code
      3. What else you can (and can’t) do with pigeon
    5. Summary
  19. Part 4: Ensuring App Quality and Stability
  20. Chapter 11: Unit Tests, Widget Tests, and Mocking Dependencies
    1. Technical requirements
    2. Getting started with unit testing
      1. Introducing a fake repository
      2. Creating test data
      3. Writing unit tests
    3. Widget testing fundamentals
      1. Implementing a golden file test
      2. Creating the CandyShopGoldenTestWidgetsPage widget
      3. Writing widget tests
    4. Integration testing fundamentals
      1. Creating a fake repository
      2. Displaying an item in the cart test
      3. Creating the widget test with interaction
    5. Mocking dependencies for effective testing
      1. Understanding mocking and mockito
      2. Writing the unit test with mockito
    6. Summary
  21. Chapter 12: Static Code Analysis and Debugging Tools
    1. Technical requirements
    2. Following coding conventions
      1. Using the dart format command
      2. Following the Effective Dart conventions
    3. Ensuring consistency and preventing bugs with static code analysis
      1. Customizing lint rule list in analysis_options.yaml
      2. Exploring lint setup strategies
      3. Using the DCM tool
      4. Creating custom lints
    4. Exploring debugging practices and tools
      1. Logging – the good, the bad, and the ugly
      2. Using assertions to catch errors in debug mode
      3. Debugging code with breakpoints
      4. Diving into the world of Flutter DevTools
    5. Summary
  22. Index
    1. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Flutter Design Patterns and Best Practices
  • Author(s): Daria Orlova, Esra Kadah, Jaime Blasco
  • Release date: September 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781801072649