Manually Spawning Isolates for Larger Computations in Dart's Flutter Framework
Repository
https://github.com/flutter/flutter
What Will I Learn?
- You will learn how to manually spawn an Isolate in Flutter
- You will learn about Ports and Messages for Isolates
- You will learn about SendPorts and RecievePorts
- You will learn about the Flutter Task Runners
- You will learn how the Flutter Platform is structured
- You will learn how to use await for to deconstruct Streams into Futures
Requirements
System Requirements:
- IDEA intellij, Visual Studio Code with the Dart/Flutter Plugins, Android Studio or Xcode
- The Flutter SDK on the latest Master Build
- An Android or iOS Emulator or device for testing
OS Support for Flutter:
- Windows 7 SP1 or later (64-bit)
- macOS (64-bit)
- Linux (64-bit)
Required Knowledge
- A fair understanding of concurrency and paralellism
- A fair understanding of Mobile development and Imperative or Object Oriented Programming
- A simple understanding of asynchronous programming
Resources for Flutter and this Project:
- Flutter Website: https://flutter.io/
- Flutter Official Documentation: https://flutter.io/docs/
- Flutter Installation Information: https://flutter.io/get-started/install/
- Flutter GitHub repository: https://github.com/flutter/flutter
- Flutter Dart 2 Preview Information: https://github.com/flutter/flutter/wiki/Trying-the-preview-of-Dart-2-in-Flutter
- Flutter Technical Overview: https://flutter.io/technical-overview/
- Dart Website: https://www.dartlang.org/
- Flutter Awesome GitHub Repository: https://github.com/Solido/awesome-flutter
Sources:
Flutter Logo (Google): https://flutter.io/
Flutter Platform Image (Google): https://flutter.io/technical-overview/
Difficulty
- Intermediate
Description
Outline and Overview
In this Flutter Video tutorial, we take a look at how we can Spawn and Use Isolates in a manual way for more advanced computations. We build out an application that pulls in 500 JSON objects from an API endpoint. We then parse and decode that JSON using our isolate. We then send the data back through a SendPort
to the main root Isolate so that it can be displayed on the screen. In our isolate, we also use the await for
loop to turn our Port Stream into a set of future values.
Outline for this Tutorial
Item 1: Isolates in the Flutter Platform
The Flutter platform naturally uses four different task runners which are delegated to four threads. The task runners are each responsible for various different types of actions. By distributing their responsibility over multiple threads, flutter is able to make sure that performance is consistent even in situations that use sub-optimal code. As a consequence of having the platform split in this manner, we can also manually delegate expensive processes and computations to Dart Isolates.
Featured here is a layout of the general Flutter platform. In the top layer, we have most of the Dart code which is mainly what gives the widget's their shapes and paints. On the bottom side, we have a base of c++ which is able to run independent of the Dart code. This includes the Dart Virtual Machine which spawns these Isolates. Non-root isolates in flutter can not schedule frame rendering events and do not have bindings which can interact with the framework in a meaningful way.
Item 2: Communicating between Isolates
Dart's Isolates are very similar to Erlang/Elixir actors. Because they are isolated from one another, they need a way to communicate. The flutter application logic runs on a main root Isolate which is separate from the spawned isolate. We are able to use augmented stream objects called Ports to communicate between Isolates.
Here is an image of the Load Isolate function that spawns the external Isolate for our application. You can see that the first line defines a ReceivePort
. Each port wraps a Dart Stream and has a unidirectional data flow. This means that the ReceivePort
is only able to receive data and the SendPort
is only able to send data. Due to this fact, when we spawn a SendPort
or a ReceivePort
, the corresponding Port is also spawned. This means that every ReceivePort
also spawns a SendPort
and vice versa. In our application, we spawn a ReceivePort
for both the isolate and the main root isolate so that we can send and receive data on both sides. We are even able to send the corresponding SendPort
through the SendPort
to give the opposing isolate access to the ports.
Item 3: Deconstructing Streams Into Futures
Since the data that is being pushed from isolate to isolate is in stream format, we are able to make use of various abstractions to manipulate this data. One of the powerful ways to manipulate streams is to convert them into Futures via the await for
loop. The await for
loop lets us iterate through the data of a stream in an asynchronous way that is similar to a normal for
loop.
In the image above, we you can see the function that we use as an entry point for the external isolate that we spawn. In this function, we take the information that is being passed into the ReceivePort
and iterate through it by using the await for
loop. We are able to snapshot the data in this way which lets us directly deal with it like we could with any other future. In this case, we just deconstruct it from the list that we are passing through so that we can then preform the JSON computation and send that data back to our Main Isolate.
The source code for this project can be found here
Information about Flutter's Task Runners can be found here
Flutter's Technical Overview can be found here
Video Tutorial
Related Videos
Projects and Series
Stand Alone Projects:
- Dart Flutter Cross Platform Chat Application Tutorial
- Building a Temperature Conversion Application using Dart's Flutter Framework
- Managing State with Flutter Flux and Building a Crypto Tracker Application with Dart's Flutter Framework
Building a Calculator
- Building a Calculator Layout using Dart's Flutter Framework
- Finishing our Calculator Application with Dart's Flutter Framework
Movie Searcher Application
- Building a Movie Searcher with RxDart and SQLite in Dart's Flutter Framework (Part 1)
- Building a Movie Searcher with RxDart and SQLite in Dart's Flutter Framework (Part 2)
- Building a Movie Searcher with RxDart and SQLite in Dart's Flutter Framework (Part 3, Final)
Minesweeper Game
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 1)
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 2)
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 3)
- Building a Mine Sweeper Game using Dart's Flutter Framework (Part 4, Final)
Weather Application
- Building a Weather Application with Dart's Flutter Framework (Part 1, Handling Complex JSON with Built Code Generation)
- Building a Weather Application with Dart's Flutter Framework (Part 2, Creating a Repository and Model)
- Building a Weather Application with Dart's Flutter Framework (Part 3, RxCommand (RxDart) and Adding an Inherited Widget)
- Building a Weather Application with Dart's Flutter Framework (Part 4, Using RxWidget to Build a Reactive User Interface)
- Localize and Internationalize Applications with Intl and the Flutter SDK in Dart's Flutter Framework
Curriculum
- Building a Multi-Page Application with Dart's Flutter Mobile Framework
- Making Http requests and Using Json in Dart's Flutter Framework
- Building Dynamic Lists with Streams in Dart's Flutter Framework
- Using GridView, Tabs, and Steppers in Dart's Flutter Framework
- Using Global Keys to get State and Validate Input in Dart's Flutter Framework
- The Basics of Animation with Dart's Flutter Framework
- Advanced Physics Based Animations in Dart's Flutter Framework
- Building a Drag and Drop Application with Dart's Flutter Framework
- Building a Hero Animation and an Application Drawer in Dart's Flutter Framework
- Using Inherited Widgets and Gesture Detectors in Dart's Flutter Framework
- Using Gradients, Fractional Offsets, Page Views and Other Widgets in Dart's Flutter Framework
- Making use of Shared Preferences, Flex Widgets and Dismissibles with Dart's Flutter framework
- Using the Different Style Widgets and Properties in Dart's Flutter Framework
- Composing Animations and Chaining Animations in Dart's Flutter Framework
- Building a Countdown Timer with a Custom Painter and Animations in Dart's Flutter Framework
- Reading and Writing Data and Files with Path Provider using Dart's Flutter Framework
- Exploring Webviews and the Url Launcher Plugin in Dart's Flutter Framework
- Adding a Real-time Database to a Flutter application with Firebase
- Building a List in Redux with Dart's Flutter Framework
- Authenticating Guest Users for Firebase using Dart's Flutter Framework
- How to Monetize Your Flutter Applications Using Admob
- Using Geolocator to Communicate with the GPS and Build a Map in Dart's Flutter Framework
- Managing the App Life Cycle and the Screen Orientation in Dart's Flutter Framework
- Making use of General Utility Libraries for Dart's Flutter Framework
- Interfacing with Websockets and Streams in Dart's Flutter Framework
- Playing Local, Network and YouTube Videos with the Video Player Plugin in Dart's Flutter Framework
- Building Custom Scroll Physics and Simulations with Dart's Flutter Framework
- Making Dynamic Layouts with Slivers in Dart's Flutter Framework
- Building a Sketch Application by using Custom Painters in Dart's Flutter Framework
- Managing State with the Scoped Model Pattern in Dart's Flutter Framework
- Using Scoped Model in a Multi-Page or Tabbed Application with Dart's Flutter Framework
Hey @tensor
We're already looking forward to your next contribution!
Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.
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. Great work.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
As always, Thank you for moderating my contribution.