Using Dart Isolates, Dependency Injection and Future Builders in Dart's Flutter Framework

in #utopian-io6 years ago (edited)

flutter-logo.jpg

What Will I Learn?

  • You will learn about Isolates and how to use them in Flutter
  • You will learn how to implement and use Dependency Injection
  • You will learn about Future Builder widgets
  • You will learn about singletons and how they are useful for Dependency Injection
  • You will learn how to make use of factory constructors

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 object oriented programming
  • A fair understanding of Mobile development and Imperative Programming
  • Basic knowledge class based programming and inheritance

Resources for Flutter and this Project:

Sources:

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

Isolate Image: https://www.dartlang.org/

Placeholder Websites Used in this Tutorial:

PlaceIMG: https://placeimg.com/

JSON placeholder: https://jsonplaceholder.typicode.com/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Flutter Video Tutorial, we build out a application that allows us to display Photos onto a Gridview through a Future Builder widget. The photos are obtained from two separate APIs, one for Mock data and the other one for Production Data. We use the Dependency Injection Development pattern to make it easy to switch between the two Data Repositories and we also make use of Isolates in a way that allows us to get our HTTP and JSON data asynchronously.

Outline for this Tutorial
Item 1: Dart's Isolates

Many parts of Flutter make use of asynchronous code through the use of Futures and Streams. A key structure for building asynchronous code inside of a Dart and Flutter application is the Isolate. An Isolate, short for Isolated process, is a technological progression of the Erlang/Elixir Actor model. Each isolate has its own memory heap and its own code. These Isolates communicate with one another by passing messages back and forth through ports.

image.png

Inside of the Dart Virtual Machine, there are many different Isolates which are running concurrently. This concept is central to Flutter and Dart and is one of the main reasons why the Virtual Machine is so naturally preformant and scalable. Inside of our Flutter application, we make use of a function called compute. This function takes a callback function and a message and spawns a new Isolate which executes the callback function.

Item 2: Dependency Injection in Flutter

Dependency injection is a very powerful abstraction in Object Oriented Programming. Often, objects are dependent on one another through internal means, but by using the Dependency Injection Pattern, we are able to make it so that these dependencies are obtained through external means.

injector.jpg

In the application from this tutorial, we used dependency injection to allow us to create multiple Repository Objects from a single Repository Abstract class. We are then able to create an Injector class like the one above which allows us to use a singleton object to inject the repository object into our view. This class follows the Factory Singleton Pattern and we are able to use this pattern to implement Dependency Injection.

Item 3: Future Builders

The repositories that we use in this application, output their data wrapped in a Future. We can use this to our advantage in the view layer of our application by making use of a Future Builder Widget. The Future Builder Widget looks at snapshots of the selected Future and we are able to make decisions on what should be displayed based on the state of this Future.

future_builder.jpg

You can see the logic that we make use of in this Future Builder Widget. We check to see if we have an error in our Future. If the Future has an error then we print that error to the terminal. Then we check to see if we have the data in the Future. If there is no data in the Future, we display a Circular Progress Indicator Widget. If there is Data, then we are able to inflate the PhotoGrid Widget which displays our data.

Project GitHub Repository:

The Source Code for this project can be found here

Video Tutorial

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Sort:  

Hey @tensor

We're already looking forward to your next contribution!

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

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

Thank you for your contribution @tensor.
It has been reviewed and scored.


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

[utopian-moderator]

Awesome post!! Keep it up and check out THIS POST as well as I have something similar.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63900.40
ETH 3140.82
USDT 1.00
SBD 3.98