Dart Apprentice Beyond The Basics
Dart Apprentice Beyond The Basics
John Adel
1 String Manipulation
Note: Strings are case sensitive.
Trimming:
to remove the whitespace from the beginning and the end of a string use the method: trim()
to remove the whitespace from the beginning of a string use the method: trimLeft()
to remove the whitespace from the end of a string use the method: trimRight()
Padding:
padLeft(2, ’0’)method: the 2 means you want the minimum length to be two characters
long. that means that the length of the whole string is 2 characters long so the padLeft adds a
zero to the beginning of the string so if your string already contains one character the padLeft
method adds 0 to the beginning of the string making it two characters long. And the ’0’ is
the padding character you want to use. If you hadn’t specified that, the padding would have
defaulted to the space character.
and of course there is the padRight method which add characters to the end of the string.
Replacing:
replaceAll method: is used to replace all occurrences of a specific substring within a string
with another substring. (chatGPT)
replaceFirst method: is used to replace the first occurrence of some pattern.
Building Strings:
Note: Dart strings are immutable (can’t be changed) so every time you add tow strings to-
gether, Dart has to create a new object for the concatenated sting.
buffer: refers to a storage area you can modify in the computer’s memory.
String Validation: verifying that user text input is in the proper form.
startWith() method: it is used to check if the string begins with a certain letter or not.
endsWith() method: it is used to check if the string ends with a certain letter or not.
contains() method: it is used to check the middle of a string.
1
Regular expressions: sometimes called regex, express complex matching patterns in an
abbreviated form.
Special Regular Expression (regex) Character:
dot character(.): in regex, the dot character matches any single character.
question mark character(?): is a special regex character that optionally matches the
character before it.
plus sign character(+): means the character it follows can occur one or more times.
asterisk(*): means the character it follows can occur zero or more times.
square brackets([]): define a character set, which allows you to match any one character
from a specific set of characters. (chatGPT)
dash character(-): you can use it to specify ranges inside the brackets.
caret character(ˆ): The caret ˆ asserts the start of a string. For example, ˆabc matches
”abc” only if it appears at the beginning of a string. Inside square brackets, The caret
negates the character set. For example, [ˆabc] matches any character except a, b, or c.
braces character({}): are used in regular expressions to specify the exact number or
range of occurrences for a preceding character or group.
comma character(,): is used to separate the minimum and maximum number of oc-
currences in a range.
2
2 Anonymous Functions
anonymous functions: they are functions without names. In fact, they’re simply values. An
anonymous function is a function value.
Note: anonymous function can be assigned to variables and pass them around as arguments
just as you would any other value.
higher-order functions: they are functions that return functions or accept them as parame-
ters.
Note: iterable classes in Dart come predefined with many methods that take anonymous func-
tions as parameters.
Mapping: transform every value into a new one. For example: squaring.
Filtering: allows you to remove elements from a collection such as filtering even numbers.
Note: the map method is a method the is present in all of the collections(Map, List, Set) that
produces an iterable.
where method: you can filter an iterable collection like List and Set but not Map(unless you
access the keys or values of a map) using it.
reduce method: it is used to combine all the collection elements into one value.
fold method: it is used as the reduce method but with an extra parameter that provides a
starting value for the function.
imperative programming: telling the computer exactly how to calculate result you want.
callback functions: they are functions that handle events like pressing a button.
void callback: it is an anonymous function that doesn’t take any parameters or return a value.
value setter callback: A function (callback) that is used to assign a value to a variable or
property. It’s often called when you want to update or ”set” a value.(chatGPT)
3
value getter callback: A function (callback) that is used to retrieve or ”get” the current
value of a variable or property. It’s often called when you want to access the value.(chatGPT)
tear-off: is a Dart feature that allows you to pass a reference to an existing function or method
without executing it. Instead of calling the function immediately, you can refer to it and pass
it around as an argument or assign it to a variable.(chatGPT)
closure: means that the code ”closes around” the surrounding scope and therefore has access
to variables and functions defined within the scope.
4
3 Inheritance
Inheritance: is a feature that allows one class (called a child or subclass) to inherit properties
and methods from another class (called a parent or superclass). This allows the child class to
reuse code from the parent class and add its own unique features or override existing ones.
5
4 Abstract Classes
concrete classes: it means that you can make actual objects out of these classes.
Note: you begin to notice patterns and more generalized characteristics of the classes you’re
writing. So when you come to the point of just wanting to describe the general characteristics
and behavior of a class without specifying the exact way the class is implemented, you’re ready
to write abstract classes.
6
5 Interfaces
Business Logic: refers to the essence of what your app does. For example: The business logic
of a calculator app would be the mathematical calculations themselves. Those calculations
don’t depend on what your UI looks like or how you store the answers.
protocol: it is another word for interface and they are the rules for how communication hap-
pens among the users of the protocol.
Note: there’s no interface keyword in Dart. Instead, you can use any class as an interface.
Since only the field ad method names are important, most interfaces are made from abstract
classes that contain no logic.
Note: a repository is a common term to call an interface that hides the details of how data is
stored and retrieved.
single inheritance: A class inherits from only one parent class, gaining its properties and
methods.(chatGPT)
multiple inheritance: A class inherits from more than one parent class, combining properties
and methods from all the parents.(chatGPT)
7
6 Mixins
Mixins: they’re a way to reuse methods or variables among otherwise unrelated classes.
8
7 Extension Methods
extension methods: these methods allow you to add functionality to existing classes.
9
8 Generics
generics: refers to generalizing specific types so you can handle them all similarly.
binary tree: is one of the simplest types of trees. It consists of nodes, where each node can
have up to two children.
nodes description: A node with children is called a parent node, and the children are dif-
ferentiated by calling them the left child and the right child. In addition to having children,
nodes also store a value.
heap: it is the data structure where a list stores the values of a tree.
base case: in programming, especially in recursion, is the simplest condition or scenario that
stops the recursion. It provides a direct answer without requiring further recursive calls, pre-
venting infinite loops.(chatGPT)
B inary S earch T ree (BST): in a BST, the left child must always be less than the value
of its parent, while the right child is always grater than or equal to the parent. It’s also an
example where you don’t want to allow just any type.
10
9 Enhanced Enums
enums: they’re are enums because they’re enumerated values with names. they’re a way to
give names to a fixed number of options.
Usage of enums:
enums are great when you have a fixed number of options you want to represet. Her are some
examples of that:
Note: enums are just classes, and enum values are instances of the class.
operator overloading: is a feature in programming that allows you to define or change the
behavior of operators (like +, -, *, etc.) for user-defined types (such as classes or structs). This
enables the operators to work with objects of those types in a way that’s consistent with their
intended use. (chatGPT)
11
10 Error Handling
Exception: it is something outside of the usual rules.
error: not handling an exception. (that is not a good definition for an error don’t take it at all)
stack trace: is a printout of all the method on the call stack when an error occurs.
breakpoint: the red dot that is present before the numbers in VScode. when you click it,
execution will pause when it reaches the line.
12
11 Concurrency
jank: that annoying stutter that happens when the device does so much work that some ani-
mation frames get dropped.
Note: Long-running tasks generally fall into two categories: I/O tasks and computationally
intensive tasks.
input-output (I/O): includes reading and writing files, accessing a database or downloading
content from the internet.These all happens outside the CPU, so the CPU has to wait for them
to complete.
computationally intensive tasks: happen inside the CPU. These tasks might include de-
crypting data, performing a mathematical calculating or parsing JSON.
Parallelism: is when multiple tasks run at the same time on multiple processors or CPU cores.
concurrency: is when multiple tasks take turns running on a single CPU core.
The problem with parallelism: one thread saves a value in memory and expects that value
to be the same when the thread checks the value later. However, if a second thread modifies
the value, the first thread gets confused. It can be a major headache to track down those kinds
of bugs because they come from a source completely separate from the code that reports the
error. A language that supports multithreading needs to set up a system of locks so values
won’t change at the wrong time.
synchronous code: executes each instruction in order, one line of code immediately following
the previous one.
asynchronous code: which means not together in time. Asynchronous code reschedules cer-
tain tasks to run in the future when the thread isn’t busy.
Note: Dart employs concurrency on a single thread and it also uses what is calls an event loop
to execute tasks that had been postponed.
Note: the event loop uses a data structure called a queue. Think of a queue like waiting in line
at the grocery store. When you first join the line, you stand at the back of the line. Then, you
slowly move to the front of the line as people before you leave. The first one in line is the first
to leave. For that reason, developers call a queue a first-in-first-out, or FIFO, data structure.
dart uses queues to schedule tasks to execute on the main isolate.
Note: the event loop has two queues: an event queue and a microtask queue.
13
event queue: is for events like a user touching the screen or data coming in from a remote
server.
microtask queue: Dart primarily uses the microtask queue internally to prioritize certain
small tasks that can’t wait for the tasks in the event queue to finish.
Note: when it is said that Dart is single-threaded, they mean Dart only runs on a single thread
in the isolate. However, that doesn’t mean you can’t have tasks running on another thread.
Note: passing a block of code to Future causes Dart to put the code on the event queue rather
than running it synchronously.
14
12 Futures
Future: Dart’s Future type is a promise to complete a task or give you a value in the future.
Note: there are two ways to get at the value after a future completes. One is with call-
backs(an anonymous function that will run after some event has completed), and the other is
with async-await.
Note: In the case of a future, there are three callback opportunities: then, catchError and
whenComplete
Note: if a function uses the await keyword anywhere in its body, it must return a Future and
add the async keyword before the opening curly brace.
15
13 Streams
stream: it represents multiple values that will arrive in the future. It’s much like a list of
futures.
Note: the size of the chunks depends on how Dart is implemented on the system you’re using,
but it’s probably 65,536 bytes per chunk.
Note: like futures, stream events can also include an error rather than a value.
stream constructors:
Stream.empty: A stream with no values or errors. It’s done as soon as you listen to it.
generator: it is a function that produces multiple values in a sequence. Dart has two types
of generators: synchronous and asynchronous.
Note: a synchronous generator returs its values as an iterable. While an asynchronous gener-
ator returns its values as a stream.
Note:
sink: it is the way to add data or errors to a stream. It can also be called an event sink.
16
14 Isolates
Note: isolates still can’t modify the mutable objects in other isolates, but they can share
references to the same immutable objects.
Note: Isolates can only communicate by sending messages. To send a message, you need a send
port and a receive port. Picture a receive port like an audio speaker that listens for messages
and plays them when they come. Every receive port has a send port, which you can picture
as a microphone connected by a long cord back to the receive port. Message communication
happens only in one direction. You send messages with the send port and listen to them with
the receive port. There’s no way to use the receive port to send messages to the send port.
Note: calling Isolate.exit sends your message over the send port and then shuts the isolate
down.
Note: you can use both sleep and Future.delayed to pause your program. But sleep is
synchronous, so it will block all execution of other code for the full duration you specify. If you
used it in an app with a user interface, your app would become unresponsive during that time.
17