Flutter Cheat Sheet

Posted : admin On 1/3/2022

MobX has a pretty small API surface and tries to get out of your way as much as possible. Although minimal, there are few concepts youshould be familiar with. This guide is meant to be a crash course in MobX.

Required Packages

Make sure to have the following packages installed for working effectively with MobX and Flutter:

The main MobX package that includes Observables, Actions, and Reactions

Provides the Observer widget that auto-renders when the tracked observables change.

A powerful code-generator that greatly improves the developer experience with MobX. It provides annotations like@observable, @computed, @action which hides all the boilerplate in a separately generated file, *.g.dart.

These packages should appear in your pubspec.yaml like below.

Declaring a Store class

Every store in MobX should be declared with the following boilerplate. This is probably the only boilerplate thatgets repeated. You could make this into a code-snippet in your IDE. Below, we are declaring a Todo store.

Flutter and dart cheat sheet. Built with and by LESNITSKY. Contributions are very welcome! Flutter create -org com.yourorg yourproject Healthcheck.

Flutter cheat sheet pdf

Note that the basename of the part-file must match the containing-file exactly! In the above case, the partfile is called todo.g.dart, which matches the todo.dart file in which it is contained. To generate the part-file,you have to run the following command:

This tutorial should serve as a cheat sheet for named route navigation, anything from setup to waiting for results. Most of the code will be similar to the Navigator direct routing tutorial the only difference being that we'll be using named routing instead. Flutter’s online documentation is a great source of information, but sometimes you’d rather glance quickly at an example. This cheat sheet has some sample code. Just grab it and go! (To grab this code, visit All My Code.).

Ya, it looks like a mouthful 🙃 but it does the job!

Adding @observable, @computed, @action

Observables are the reactive state of your store and Actions are semantic operations that mutate them. ComputedObservables are read-only properties that depend on other observables and auto-update when any of the dependentobservables change.

Reactive wrappers

MobX also comes with a set of wrapper-classes that add the reactive behavior. These include:

  • ObservableList<T>
  • ObservableSet<T>
  • ObservableMap<K,V>
  • ObservableFuture<T>
  • ObservableStream<T>

Reactive Extensions

Flutter Cheat SheetSheet

You can convert plain List, Map, Set, Future and Stream instances into an observable version with the asObservable() extension method.For example, in the code-snippet above, you could do:

Don't underestimate the @computed

Although @computed looks like a simple, readonly observable, it can easily be your most powerful tool.By creating @computed properties that depend on other observables, you can dramatically simplify the UI code andeliminate most of the business logic inside your Widgets. It is most often used for hiding conditional logic andcalculating some derived information.

For example, rather than checking if some data is loaded successfully inside a Widget...

...you can create a @computed property called hasResults...

...and simplify your widget logic...

...Since a @computed property is an observable, the Observer will automatically render when it changes!

Flutter cheat sheet

Adding reactions

Reactions, as the name suggests, react to changes in observables. Without reactions, it would be a boring systemthat only produces changes in observables but nothing visible or useful ever happens because there are no reactions!

There are 3 types of reactions: autorun, reaction, when. Each reaction returns a ReactionDisposer, which when called will disposethe reaction. Disposing a reaction stops tracking the observables.

  • autorun: Is a long-running reaction and starts tracking immediately.
  • reaction: Is a long-running reaction and starts tracking only after the first change. It runs the effect whenany tracked observables change.
  • when: a reaction that waits for a condition to become true before running the effect. After running the effect,it automatically disposes. Thus when is a one-time only reaction!.
  • Using literals
  • Chaining method calls
  • Dealing with null
  • Implementing functions
  • Implementing constructors

This cheatsheet is based on an internal documentcreated by Googler Mehmet Fidanboyluto help Google engineers remember the syntax forsome of Dart’s commonly used features.For an interactive guide to these features and more, see theDart cheatsheet codelab.

Using literals

‘Substitution ${val}’

Puts the value of val into a string literal.Equivalent: ‘Substitution’ + val

<type>[ ]

Flutter Container Decoration

Creates an object of type List<type>.Equivalent: new List<type>();

const [1, 2, 3]

Creates a compile-time constant list.

= { }

Initializes a map.Equivalent: new Map<>();

Flutter Cheat Sheet Github

Declaring fields

varGeneric var (dynamic)
finalSame as var but final
constConstant

Checking types

asTypecast
isinstanceof
is!!instanceof

Chaining method calls

a..b = true..c = 5;

Cascade used for chaining access to methods and other members.Equivalent: a.b = true; a.c = 5;

Dealing with null

b ??= val;

If b is null, assign the value of val to b;otherwise, b stays the same.

a = value ?? 0;

If value is null, set a to 0.Otherwise, set a to value.

a?.b

Conditional access.Equivalent: a null ? null : a.b

Implementing functions

fn({bool bold: false, bool hidden: false})

Named params with default values.

int incr(int a) => a + 1;

Single return statement can be abbreviated.

Handling exceptions

Use on to catch a type.Use catch to catch an instance.

Implementing constructors

Flutter Layout Cheat Sheet

Normal constructor

Factory constructor

Flutter Cheat Sheet Pdf

Use factory when implementing a constructor thatdoesn’t always create a new instance.

Named constructor

Delegating constructor

Flutter Column Cheat Sheet

Const constructor

Produces an object that will never change. All vars have to be final.

Initializer list

Flutter Layout Cheat Sheet

Initializer lists are handy when setting up final fields.