Dart Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
Dart Introduction
Exercise
Print a line.
("Hi");Dart Get Started
Exercise
Run a file.
dart hello.dart
Dart Syntax
Exercise
End a statement.
print("a")Dart Variables
Exercise
Compile time constant.
max = 10;Dart Data Types
Exercise
Whole number type.
n = 5;Dart Strings
Exercise
Insert a variable.
"Hi name"
Dart Null Safety
Exercise
Allow null.
String s;
Dart Type Conversion
Exercise
Safe parse.
int.("x")
Dart Input and Output
Exercise
Read a line.
stdin.()
Dart Comments
Exercise
Doc comment prefix.
Adds two numbers.Dart Booleans
Exercise
Logical not.
okDart Math
Exercise
Largest of two.
(3, 9)More String Methods
Exercise
Pad with zeros.
"7".(3, "0")
Dart Date and Time
Exercise
Add time to a date.
d.(Duration(days: 1))
The main Function
Exercise
Entry function.
void () {}
var, final and const
Exercise
Set once.
x = 5;Built in Types
Exercise
Either int or double.
n = 3;Null Safety
Exercise
Default.
x "none"
String Interpolation
Exercise
Insert an expression.
"${a 1}"
Type Conversion
Exercise
Safe parse.
int.("5")
dart:math
Exercise
Square root.
(16)Operators
Exercise
Integer division.
7 2
Cascade Notation
Exercise
Cascade operator.
objmethod()
Conditional Expressions
Exercise
Ternary.
c a : b
Comments and Documentation
Exercise
Doc comment.
Adds numbersDart Operators
Exercise
Integer division.
17 5
Assignment and Null Aware Operators
Exercise
Assign if null.
x 5;
Dart If and Else
Exercise
Second branch.
if (a) {} if (b) {}
Dart Switch
Exercise
Fallback in an expression.
switch (x) { => 0 }
Dart Loops
Exercise
Loop over a list.
for (var x list)
if and else
Exercise
Else branch.
if (x) {} {}
switch Statements and Expressions
Exercise
Default arm.
=> "many"for and for-in Loops
Exercise
Iterate a list.
for (var x list)
while and do while
Exercise
Run once first.
{ } while (c);break, continue and Labels
Exercise
Skip a round.
;Collection if and for
Exercise
Conditional item.
[1, (c) 2]
Pattern Matching and Records
Exercise
Destructure a pair.
var (a, ) = (1, 2);
Dart Lists
Exercise
Add an item.
list.("x")
Dart Sets and Maps
Exercise
Check for a key.
map.("x")
Collection Methods
Exercise
Keep matching items.
n.((x) => x > 2)
Sorting and Iterables
Exercise
Keep the first 2.
n.(2)
Lists
Exercise
Add an item.
a.(5)
List Methods
Exercise
Join with a dash.
a.("-")
Sets
Exercise
Common items.
a.(b)
Maps
Exercise
Check a key.
m.("a")
Spread Operators
Exercise
Spread.
[0, a]
Sorting and Comparing
Exercise
Custom order.
w.sort((a, b) => a.(b))
toList, toSet and expand
Exercise
Remove duplicates.
a.().toList()
Queue and Iterable
Exercise
Add to the front.
q.(0)
String Methods
Exercise
Split text.
s.(",")
Regular Expressions
Exercise
Test a pattern.
re.("text")
Dart Functions
Exercise
Arrow syntax.
int sq(int x) x * x;
Optional and Named Parameters
Exercise
Mark a named parameter mandatory.
{ int times}
Closures and Function Values
Exercise
Function type.
f = () => 1;Functions
Exercise
Arrow body.
int f(int x) x * 2;
Named and Optional Parameters
Exercise
Required named.
({ int w})
Closures and Function Types
Exercise
Function type.
int (int) f
Higher Order Functions
Exercise
Filter.
n.((x) => x > 2)
Recursion
Exercise
Base case.
if (n <= ) return 1;
typedef and Callbacks
Exercise
Name a type.
Op = int Function(int);Generators: sync* and async*
Exercise
Produce a value.
i;Dart Classes and Objects
Exercise
Set a field in the constructor.
Student(.name);
Dart Constructors
Exercise
Named constructor form.
Point.() : x = 0
Getters, Setters and Privacy
Exercise
Private marker.
int count = 0;
Dart Inheritance
Exercise
Inherit.
class Dog Animal
Abstract Classes and Interfaces
Exercise
Promise a contract.
class A Shape
Mixins
Exercise
Use a mixin.
class A M
Enums and Records
Exercise
Declare an enum.
Color { red }Classes
Exercise
Initialising formal.
Dog(.name);
Constructors
Exercise
Named constructor.
Point.() : x = 0
Getters and Setters
Exercise
Computed property.
double area => w * h;
Inheritance
Exercise
Inherit.
class Cat Animal
abstract Classes and Interfaces
Exercise
Cannot instantiate.
class Shape {}Mixins
Exercise
Use a mixin.
class A M {}
Enums
Exercise
Enum keyword.
Level { a, b }Static Members
Exercise
Class level.
int n = 0;Generics
Exercise
Type parameter.
class Box<> {}
Extension Methods
Exercise
Extend String.
Shout on String {}toString, == and hashCode
Exercise
Text of an object.
String () => "x";
Operator Overloading and Callable Classes
Exercise
Overload plus.
V operator (V o)
Dart Exceptions
Exercise
Catch a specific type.
FormatException catch (e)Dart Files
Exercise
Read all text.
await f.()
try, catch and finally
Exercise
Always runs.
try {} {}
throw and Custom Exceptions
Exercise
Raise.
AgeError("x");Futures and async/await
Exercise
Wait for a future.
load()Streams
Exercise
Consume a stream.
await (var x in s)
Files with dart:io
Exercise
Read lines.
f.readAsSync()
JSON with dart:convert
Exercise
To JSON.
(obj)Dates and Durations
Exercise
Add time.
d.(Duration(days: 1))
Null Aware Operators
Exercise
Assign if null.
a "x"
Practice: Word Frequency
Exercise
Default 0.
(freq[w] 0) + 1
Practice: FizzBuzz
Exercise
Pattern for a pair.
(0, ) => "Fizz"
Practice: Bank Account
Exercise
Private field.
double balance = 0;
Dart Generics
Premium exercise. See plans
Futures, async and await
Premium exercise. See plans
Streams
Premium exercise. See plans
Isolates
Premium exercise. See plans
Extension Methods
Premium exercise. See plans
Patterns and Sealed Classes
Premium exercise. See plans
Flutter Basics
Premium exercise. See plans
Flutter State
Premium exercise. See plans
Flutter Layout
Premium exercise. See plans
HTTP and JSON
Premium exercise. See plans
Packages and pubspec
Premium exercise. See plans
Testing Dart
Premium exercise. See plans
Dart Best Practices
Premium exercise. See plans
Building and Publishing
Premium exercise. See plans
assert
Exercise
Check a condition.
(age >= 0);late Variables
Exercise
Lazy init.
final String s;Characters and Runes
Exercise
Char code.
String.(65)
Type Tests and Casts
Exercise
Test a type.
o String
Iterable Helpers
Exercise
First match.
a.((x) => x > 3)
const and Unmodifiable Collections
Exercise
Immutable.
a = [1, 2];Command Line Arguments
Exercise
Argument list.
void main(List<> a)
Libraries and Imports
Exercise
Prefix.
import "x" m;