dart basics Flashcards

1
Q

“Hello world” in Dart

A

void main() { print(‘hello world’); }

void main() { print('hello world'); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the package manager CLI tool for Dart?

A

pub

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Embed the variable “name” in a welcome string!

A
String welcome = "Hello ${name}!";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Print the runtime type of the variable named “data”!

A
print(data.runtimeType);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

4 ways of variable type assignment in Dart

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

2 ways of foreach in Dart

A

functional (.forEach)

imperative (for-in)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are Future and Stream Objects?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the functional asynchronous equivalent pattern of a try catch block?

A

.then().catchError()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to wait for many parallel functions to all finish, then continue?

A
await Future.wait([deleteLotsOfFiles(), copyLotsOfFiles(), checksumLotsOfOtherFiles(),]); print('Done with all the long steps!');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Deferred loading (also called lazy loading)

A

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();}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

2 ways of handling streams

A
  • Use async and an asynchronous for loop (await for).
  • Use the Stream API, as described in the library tour.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

2 kinds of generator functions

A
  • 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++;}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are isolates?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

4 major libraries of Dart

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to create multiline sring in Dart?

A

String s = ‘’‘

all

of this

is one string

’’’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Import package with pre-made material design widgets

A
**import**'package:flutter/material.dart'**;**
17
Q
A