dart basics Flashcards
“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 reduce an app’s initial startup time.
- To perform A/B testing—trying out alternative implementations of an algorithm, for example.
- To load rarely used functionality, such as optional screens and dialogs.
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
- Use async and an asynchronous for loop (await for).
- Use the Stream API, as described in the library tour.
2 kinds of generator functions
- Synchronous generator: Returns an Iterable object.
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++;}
- Asynchronous generator: Returns a Stream object.
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
- Built-in types, collections, and other core functionality. This library is automatically imported into every Dart program.
dart:async
- Support for asynchronous programming, with classes such as Future and Stream.
dart:math
- Mathematical constants and functions, plus a random number generator.
dart:convert
- Encoders and decoders for converting between different data representations, including JSON and UTF-8.
How to create multiline sring in Dart?
String s = ‘’‘
all
of this
is one string
’’’;