“Hello world” in Dart
void main() { print(‘hello world’); }
void main() { print('hello world'); }What’s the package manager CLI tool for Dart?
pub
Embed the variable “name” in a welcome string!
String welcome = "Hello ${name}!";Print the runtime type of the variable named “data”!
print(data.runtimeType);
4 ways of variable type assignment in Dart
inferred (var keyword, only inferred value can be assigned and reassigned)
static (stongly typed, only given value can be assigned and reassigned)
dynamic (used for indicating that expected type is not suitable for any given type. runtimeType is inferred and can be changed)
Object (used for indicating that any type is applicable. runtimeType is inferred and can be changed)
2 ways of foreach in Dart
functional (.forEach)
imperative (for-in)
What are Future and Stream Objects?
Dart libraries are full of functions that return Future or Stream objects. These functions are asynchronous: they return after setting up a possibly time-consuming operation (such as I/O), without waiting for that operation to complete.
What’s the functional asynchronous equivalent pattern of a try catch block?
.then().catchError()
How to wait for many parallel functions to all finish, then continue?
await Future.wait([deleteLotsOfFiles(), copyLotsOfFiles(), checksumLotsOfOtherFiles(),]); print('Done with all the long steps!');Deferred loading (also called lazy loading)
Deferred loading (also called lazy loading) allows an application to load a library on demand, if and when it’s needed. Here are some cases when you might use deferred loading:
To lazily load a library, you must first import it using deferred as.
**import**'package:greetings/hello.dart' deferred **as** hello;
When you need the library, invoke loadLibrary() using the library’s identifier.
Future greet() async {await hello.loadLibrary();hello.printGreeting();}2 ways of handling streams
2 kinds of generator functions
To implement a synchronous generator function, mark the function body as sync*, and use yield statements to deliver values:
Iterable\<**int**\> naturalsTo(**int** n) sync\* {**int** k = 0;**while** (k \< n) yield k++;}To implement an asynchronous generator function, mark the function body as async*, and use yield statements to deliver values:
Stream\<**int**\> asynchronousNaturalsTo(**int** n) async\* {**int** k = 0;**while** (k \< n) yield k++;}What are isolates?
Most computers, even on mobile platforms, have multi-core CPUs. To take advantage of all those cores, developers traditionally use shared-memory threads running concurrently. However, shared-state concurrency is error prone and can lead to complicated code.
Instead of threads, all Dart code runs inside of isolates. Each isolate has its own memory heap, ensuring that no isolate’s state is accessible from any other isolate.
4 major libraries of Dart
dart:core
dart:async
dart:math
dart:convert
How to create multiline sring in Dart?
String s = ‘’‘
all
of this
is one string
’’’;
Import package with pre-made material design widgets
**import**'package:flutter/material.dart'**;**