Introduction to the Course

Why this course?

Flutter is a cross-platform mobile app development framework. It enables developers to create apps for Android, iOS, web, and desktop platforms using a single codebase. It offers a variety of widgets and features that make it easier and quicker to create interactive and visually appealing apps.

A counter app is created by default whenever we create a new Flutter application. The app just has one function: it displays a number and provides a button to increment it.

Now, click the “Run” button below to see the default counter app in action:

Note: Upon application execution, you can visit the URL after “Your app can be found at:” to view the app. 

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
The state changes when the user clicks on the "+" button in the counter app

State in Flutter refers to the current configuration of our application. For example, when we first start the application, the counter state is 0 in the default Flutter counter application. Many events can trigger a change in state, like user input or an API response. In the case of the counter app, when the user clicks the “+” button, the state of the counter is changed from 0 to 1.

In almost all Flutter applications, this state needs to be consistent throughout the application. There are two approaches to go about this:

  • Use a chaotic, spaghetti-style implementation of state management that is difficult to trace and will break with even the smallest change in the code.

  • Applying a state management approach to make our code robust to errors, as well as simple to debug and catch mistakes.

The second option is the best one to take! Nobody enjoys wasting hours attempting to decipher a piece of code to fix a bug, right?

This course aims to teach you Business Logic Components (BLoCs). It's a pattern for handling application state that aims to simplify development by decoupling our app’s logic from the UI.

Prerequisites

To take this course, it’s essential to have a basic understanding of Flutter fundamentals, such as:

  • Dart basics

  • Creating a Flutter application

  • Stateful and stateless widgets

  • Understanding alignment

  • Connecting to the backend

You’re in the right place if you know how to make a to-do list app that can send and get data from a backend (like Firebase).

If you’re new to Flutter, don’t fret! You can check Appendix A for resources to learn Flutter from the beginning!

Course structure

A diagram that visualizes the course structure
A diagram that visualizes the course structure

Understanding the BLoC pattern

This course will first provide an overview of the BLoC pattern. We’ll create an application that utilizes the BLoC pattern without any helper libraries. Understanding this will help us understand the use of libraries to build the application.

The BLoC library

After mastering the fundamentals of BLoC, we’ll move on to the BLoC library. The BLoC library assists in creating the BLoC pattern faster and with less boilerplate code.

The BLoC library contains many widgets that can be used in various ways. We’ll additionally cover the following topics:

  • The fundamentals of the BLoC library

  • How to use the BLoC library to implement the repository pattern

  • What BLoC cubits are and how to use them

  • How to test the BLoC library

RxDart

RxDart is another library built with the BLoC architecture. We’ll learn how to use it. RxDart provides additional functionalities to Dart Streams called Subjects. These Subjects simplify the implementation of the BLoC pattern and give us more control over our BLoC.

Best practices

We’ll learn some of the best practices that can be used while working with BLoC. This will help us write cleaner code and get a better grasp of when and where BLoC is most useful.

Course projects

There are two projects in this course. We’ll build these together, step by step, to guide you when you’re first learning how to use BLoC. Additionally, there are two challenges at the end of the course that will test your knowledge of building apps with the Flutter BLoC library and RxDart. The solutions to these challenges will be provided in separate lessons so that you can compare your solution to ours.