Learn the basics for the dart programming language, which is used to build cross platform app using flutter.
Variables
Dart syntax is mostly like C++
Dart also has var, const and final. But there not anything like JS
var: it's like any in TS
final works like const in JS
const : Can only set value once and the value should be know at the build time
Null Safety
In Dart a variable cannot be assigned to null by default.
Late
When using classes, you want to declare a variable, but set its value later in the code
Add the late keyword
late allows to keep the variable as non-nullable value but you can initialize it later.
Operators
Assigns the value "Guest" only if name is null.
Cascade
Cascades (.., ?..) allow you to make a sequence of operations on the same object. In addition to accessing instance members, you can also call instance methods on that same object.
The constructor, Paint(), returns a Paint object. The code that follows the cascade notation operates on this object, ignoring any values that might be returned.
It is equivalent to
Functions
Positional Arguments
Named Arguments
You can pass the arguments in any order, the arguments are non-nullable. You can use optional, required and default values.
Class
It provides a way to create object of complex types.
We can also define static methods that don't need objects for there execution
Constructor
To initialize a class variable that depends on the value passed to the constructor, use the following syntax
Passing optional parameters
Using Named parameters
NOTICE: In case of Named arguments, we didn't need to define the class properties since they are already named.
Named Constructors
Used when use want to initialize the constructor data by passing different types of arguments
Extends
Used for implementing inheritance in dart
Mixins
When extending classes is not enough and you want to add additional behaviors
Generics
It allows you to pass a type as a parameter
Packages
Importing package
Import package with a different namespace
Hide a certain class
Only use 1 class from the package
Asynchronous programming
Futures
Futures are just a re-branding of Promises from the JS world
You can also use async, await syntax
Example
In flutter there are dedicated widget (future builder), that are used to resolve a future directly in the UI.
Streams
Is like promise.all(), It is used to handle multiple async events and handle them from the same place, as they get resolved over time.
A stream is like a list of values that will come with time.
And you can also use methods like map, reduce, ...
By default you can only listen to a stream once. To listen to a stream multiple times convert it to a broadcast stream
Using async/await with streams
Just like FutureBuilder, flutter has StreamBuilder to resolve multiple async request in the UI