Dart Programming Basics

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is Dart primarily used for?

  • Building mobile, web, and server applications. (correct)
  • Data analysis and statistical modeling.
  • Creating operating systems and device drivers.
  • Managing databases and complex queries.

Which compilation methods does Dart support to optimize performance in different scenarios?

  • JIT (Just-In-Time) compilation only.
  • AOT (Ahead-of-Time) compilation only.
  • Both AOT and JIT compilation. (correct)
  • Neither AOT nor JIT compilation.

What is the primary difference between single-line comments (//) and multiline comments (/* ... */) in Dart?

  • Single-line comments apply only to the current line, while multiline comments can span multiple lines. (correct)
  • Single-line comments are processed by the Dart compiler, while multiline comments are ignored.
  • There is no functional difference; they are interchangeable.
  • Single-line comments are used for documentation, while multiline comments are for code explanations.

What is the purpose of documentation comments (///) in Dart, and how do they differ from regular comments?

<p>Documentation comments support special syntax for linking to API documentation using brackets (<code>[]</code>). (D)</p> Signup and view all the answers

What is the significance of the arrow syntax (=>) in Dart functions?

<p>It is used as a shorthand for a function body that contains only one expression. (C)</p> Signup and view all the answers

In Dart, what is the difference between declaring a variable with var and explicitly specifying its type (e.g., String, int)?

<p>Using <code>var</code> infers the variable type automatically; explicit typing requires you to define it. (D)</p> Signup and view all the answers

What distinguishes final from const when declaring variables in Dart?

<p><code>final</code> variables can be assigned only once at runtime; <code>const</code> variables must be known at compile time. (C)</p> Signup and view all the answers

How do you declare a variable as private in Dart?

<p>Prefix the variable name with an underscore (<code>_</code>). (B)</p> Signup and view all the answers

Which of the following is a valid way to concatenate strings in Dart?

<p>Using the <code>+</code> operator or adjacent string literals. (C)</p> Signup and view all the answers

How does Dart represent Unicode values in strings, and why is this important?

<p>Using UTF-16 code units with a special syntax (<code>\uXXXX</code>) to handle a wide range of characters. (B)</p> Signup and view all the answers

Given a List called myList, how can you access the first element?

<p><code>myList[0]</code> (C)</p> Signup and view all the answers

What is the key characteristic of a growable List in Dart?

<p>Its size can dynamically increase or decrease as elements are added or removed. (A)</p> Signup and view all the answers

When using Maps in Dart, what constraint applies to the keys?

<p>Keys must be unique within the Map. (D)</p> Signup and view all the answers

In Dart, what is the purpose of the is and is! operators?

<p>To test if an object is of a specified type. (C)</p> Signup and view all the answers

What does the operator ??= do in Dart?

<p>It assigns a value only if the variable is currently null. (C)</p> Signup and view all the answers

How does the cascade notation (..) enhance code efficiency in Dart?

<p>It allows multiple operations on the same object without repeating the object's name. (A)</p> Signup and view all the answers

What is the purpose of the ternary operator in Dart, and when is it most appropriate to use?

<p>To conditionally execute one of two expressions based on a boolean condition. (C)</p> Signup and view all the answers

What is the primary difference between the while and do-while loops in Dart?

<p>The <code>while</code> loop checks the condition before executing the loop body, while the <code>do-while</code> loop checks after. (B)</p> Signup and view all the answers

In Dart, what is the purpose of the break statement within a loop?

<p>To terminate the loop prematurely and continue execution after the loop. (A)</p> Signup and view all the answers

What is the function of the continue statement in Dart loops?

<p>It skips the rest of the current iteration and proceeds to the next iteration. (A)</p> Signup and view all the answers

How does the switch statement in Dart improve code readability compared to multiple if-else statements?

<p><code>switch</code> statements provide a more concise and organized way to handle multiple discrete cases. (B)</p> Signup and view all the answers

In Dart, if a function does not explicitly specify a return type and no return statement is provided, what does the function return by default?

<p>The function returns a <code>null</code> value. (D)</p> Signup and view all the answers

What is the expected purpose of using the void keyword as a return type in a Dart function?

<p>To specify that the function does not return any value. (B)</p> Signup and view all the answers

Which of the following is the correct way to import a package named my_package from a package manager in Dart?

<p>import 'package:my_package'; (C)</p> Signup and view all the answers

Why is it beneficial to separate code logic into different class files when developing with Dart?

<p>It allows for better organization and grouping of code into manageable modules. (B)</p> Signup and view all the answers

What is a key benefit of using AOT (Ahead-of-Time) compilation in Dart?

<p>It significantly reduces the app's startup time. (C)</p> Signup and view all the answers

For what purpose is JIT (Just-In-Time) compilation primarily utilized in Dart?

<p>To quickly display code changes during development, such as with Flutter's stateful hot reload. (D)</p> Signup and view all the answers

How can external classes be imported in Dart?

<p>By specifying the location and class name or the package directive. (B)</p> Signup and view all the answers

How are multiline strings created in Dart?

<p>Using triple quotes (''' or &quot;&quot;&quot;). (A)</p> Signup and view all the answers

What type of values can be assigned to a variable declared as bool in Dart?

<p>True or False. (C)</p> Signup and view all the answers

What is the proper syntax to declare an integer variable named quantity with an initial value of 10 in Dart?

<p>int quantity = 10; (A)</p> Signup and view all the answers

In a Dart for loop, how do you access the loop counter?

<p>Accessing the loop variable directly. (C)</p> Signup and view all the answers

How do you specify that the function takes an optional parameter in Dart?

<p>Enclose the parameter in square brackets [ ]. (C)</p> Signup and view all the answers

What happens if two keys with the same value are used on the same Map?

<p>The latter key will overwrite the former key. (B)</p> Signup and view all the answers

Which operator would allow for type casting in Dart code?

<p>The <code>as</code> operator. (C)</p> Signup and view all the answers

Which of the following statements is true regarding relational operators in Dart?

<p>The <code>&gt;=</code> operator checks if one value is greater than or equal to another, and <code>&gt;</code> operator checks if one value is strictly greater than the other. (C)</p> Signup and view all the answers

When constructing a function in dart, under what conditions should it start with the word void?

<p>When there is no expected return value. (B)</p> Signup and view all the answers

Flashcards

AOT Compilation in Dart

Dart is AOT compiled to native code, making Flutter apps fast without intermediary interpretation.

JIT Compilation in Dart

Dart uses JIT compilation during debugging, enabling features like Flutter's stateful hot reload.

Dart in Flutter UI

Flutter uses Dart for UI code, removing the need for separate UI languages or visual designers.

Single-Line Comments in Dart

Single-line comments start with //, ignored by the Dart compiler.

Signup and view all the flashcards

Multiline Comments in Dart

Multiline comments start with /* and end with */, with everything between ignored by Dart.

Signup and view all the flashcards

Documentation Comments in Dart

Documentation comments begin with ///, connecting to SDK documentation for HTML generation.

Signup and view all the flashcards

Variables

Variables store references to a value; built-in types include numbers, strings, Booleans, lists, maps, and runes.

Signup and view all the flashcards

Declaring Variable Type

Declaring a variable type improves code readability, showing the expected value type.

Signup and view all the flashcards

Variable Privacy in Dart

In Dart, variables are public by default, but start with an underscore (_) to make them private.

Signup and view all the flashcards

Declaring String Variables

String variables are declared using String book-Title and are uninitialized with null value

Signup and view all the flashcards

int vs double

Both int and double allow for positive and negative numbers, using 64-bit memory.

Signup and view all the flashcards

Dart List

A List is an ordered group of objects, accessed using zero-based indexing.

Signup and view all the flashcards

Dart Map

Maps connect a List of values to a Key for recalling values by their Key ID.

Signup and view all the flashcards

Dart Rune

UTF-32 code points of a String called Runes define a numeric value for each letter, digit, and symbol

Signup and view all the flashcards

! Operator

Returns the opposite value of the variable/expression

Signup and view all the flashcards

&& Operator

Returns true if the values of the variable/expression are all true

Signup and view all the flashcards

|| Operator

Returns true if at least one value of the variable/expression is true

Signup and view all the flashcards

if and else in dart.

if and else statements decides which code to run by comparing multiple scenarios.

Signup and view all the flashcards

For loop

for loops allow iterating a List of values.

Signup and view all the flashcards

Dart Functions

With dart functions can group reusable logic. A function can optionally take parameters and return values

Signup and view all the flashcards

Study Notes

Introduction to Mobile Programming - Unit 3-1: Learning Dart Basics

  • Dart serves as the foundation for developing Flutter projects.
  • A good understanding of Dart is needed before starting to develop Flutter apps.
  • Google created Dart, and it is used internally in products like Google AdWords.
  • Dart was made available publicly in 2011.
  • The language can be used to build mobile, web, and server applications.
  • Dart is known for being productive, fast, portable, approachable, and reactive.
  • Dart is an object-oriented programming (OOP) language.
  • It features a class-based style and a C-style syntax.
  • Familiarity with languages like C#, C++, Swift, Kotlin, and Java/JavaScript makes learning Dart easier.
  • Dart is straightforward to learn, even without prior experience in those languages.

Benefits of Using Dart

  • Dart is compiled ahead-of-time (AOT) into native code, which makes Flutter apps run fast.
  • There is no need to interpret one language to another.
  • AOT compilation is used when compiling apps for release to app stores like Apple App Store and Google Play.
  • Dart is also just-in-time (JIT) compiled.
  • The JIT compilation enables fast code change display via Flutter’s stateful hot reload feature.
  • JIT compilation is used during app debugging in a simulator/emulator.
  • Flutter uses Dart for UI code, eliminating the need for separate languages to create the UI.
  • Flutter rendering can run at 60 frames per second (fps) and up to 120fps on capable devices.
  • Higher fps contributes to a smoother app experience.

Commenting Code

  • Single-line comments start with "//".
  • The Dart compiler ignores everything after "//" on that line.
  • Multiline comments begin with "/" and end with "/".
  • Everything between "/" and "/" is ignored by the compiler.
  • Documentation comments start with "///".
  • The Dart compiler ignores everything on the line with "///", unless enclosed in brackets.
  • Brackets can refer to classes, methods, fields, and functions.
  • The SDK's tool (dartdoc) can parse code and generate HTML documentation.

Running the Main() Entry Point

  • There are a few ways to use the main() function.
  • One way is with arrow syntax: void main() => runApp(MyApp());.
  • A reason to use the arrow syntax is that there's no need to call multiple statements.
  • The arrow syntax => runApp(MyApp()) is the same as { runApp(MyApp()); }.

Referencing Variables

  • Variables store references to a value.
  • Built-in variable types are numbers, strings, Booleans, lists, maps, and runes.
  • The keyword var declares a variable, and Dart infers the type automatically.
  • It is better to declare the variable type for code readability and to know the expected value type.
  • Declare variables with their expected type, like double or String.
  • By default, all variables are public.
  • Start the variable name with an underscore (_) to declare it as private.

More on Referencing Variables

  • The variable String bookTitle is declared; bookTitle has no value e.g. a null value
  • However, String bookTitle = 'Beginning Flutter' assigns it the value 'Beginning Flutter'.
  • Use final or const when the variable's value is not intended to be changed.
  • Use final when the value is assigned at runtime (user can change it).
  • Use const when the value is known at compile time (in code) and will not change.

Declaring Variables (Examples)

  • var filter = 'company' means that you are declaring the variable without specifying the type, so it infers type.
  • String filter = 'company' means you are declaring by type "string".
  • String filter; means uninitialized variable has an initial value of null.
  • final filter = 'company'; means the value will not change.
  • const String filter = 'company' const example.
  • const String filter = 'company' + filterOption another way of declaring.
  • String userName = 'Sandy'; is a public variable because name doesn't start with underscore.
  • String _userID = 'XW904'; is a private variable because it does start with underscore.

Numbers in Dart

  • Declaring variables as numbers restricts the values to numbers only.

  • Dart supports int (integer) or double when declaring numbers.

  • Integer: Use the int declaration when numbers do not need decimal precision (e.g., 10 or 40).

  • Double: Use the double declaration for numbers that require decimal precision (e.g., 50.25 or 135.7521).

  • Both int and double allow for positive and negative numbers.

  • Large numbers and decimal precision are possible since they use 64-bit values.

Strings in Dart

  • Declaring variables as String allows them to be entered as text.
  • Single or double quotes adds a single line e.g. 'car' or "car"
  • Triple quotes add multiline e.g. "'car"'.
  • Strings can be concatenated by using the plus (+) operator.

Booleans in Dart

  • Declaring variables as bool (Boolean) sets the value to either true or false.

List Details

  • A List is an ordered group of objects.
  • Elements are accessed via zero-based indexing.
  • The index of the first element is 0.
  • The index of the last element is the List length minus 1.
  • Lists are growable by default, created with List() or [].
  • To create a fixed-length List, add the number of rows required using List(25).
  • String interpolation is used in the print statement: print('filter: $filter').
  • A dollar sign ($) is used before the variable to convert the expression value to a string.

More About Maps

  • Maps associate a List of values by a Key and a Value.
  • The values can be recalled by their Key ID.
  • Each Key and Value can be any data type, such as String or Number.
  • Each Key needs to be unique so the Value is retrieved by the Key.

Runes in Dart

  • Variables can be Runes using the UTF-32 code points of a String.
  • Unicode defines a numeric value for each letter, digit, and symbol.
  • The sequence of UTF-16 code units represent a 32-bit Unicode value from a string using special syntax (\uXXXX).
  • A Unicode code point is \uXXXX, with XXXX being a four-digit hexadecimal value.
  • Runes return the integer value of the Unicode.
  • String.fromCharCodes() allocates a new String for the specified charCode.

Dart Operators

  • Arithmetic Operators:
    • Addition (+), Subtraction (-), Multiplication (*), Division (/).
  • Equality and Relational Operators:
    • Equal (==), Not equal (!=), Greater than (>), Less than (<),
    • Greater than or equal to (>=), Less than or equal to (<=).
  • Type Test Operators:
    • as (Typecast with import library prefixes), is (True if object contains specified type), is! (False is object contains specifies type).
  • Assignment Operators:
    • Assigns value (=) Assigns value, Assigns value only if variable being assigned to has a value of null (??=),
    • Adds to current value (+=), Subtracts from current value (=), Multiplies from current value (*=), Divides from current value (/=).
  • Logical Operators:
    • "not" (!), "and" (&&), "or" (||).
  • Conditional expressions:
    • Checks if condition is true/false and returns the values.
  • Cascade notation (..):
    • Double dots (..) represents cascade notation.
    • Allows a sequence of operations on the same object.

Flow Statements: Controlling Logic Flow in Dart

  • The most common flow statements are if and else.
  • They decide which code to run by comparing multiple scenarios.
  • The ternary operator is like if and else, but used when only two choices are possible.
  • for loops allow iterating a List of values.
  • while and do-while are common pairs.
  • Use while to evaluate the condition before the loop; use do-while to evaluate after.
  • while and break are useful to stop evaluating the condition in the loop.
  • continue is for stopping the current loop and starting the next loop iteration.
  • switch and case are alternatives to if and else, but require a default clause.

If-Else Statements

  • When checking boolean values in if statements, the statement checks if the variable is true by default.
  • Code equivalent to isClosed == true.
  • To check if a variable is false, use either:
    • The not equal (!=) operator (e.g. isClosed != true).
    • The equality (==) operator (e.g. isClosed == false).

Ternary Operator

  • The ternary operator uses three arguments.
  • Used when only two actions are needed.
  • Checks the first argument for comparison.
  • If the first argument is true, action after question mark.
  • If the first argument is false, action after colon.

For Loops in Dart

  • For iterating of loop and going through string values
  • The for in loop takes one parameter that exposes a object

While & Do-While Loops

  • The while loop iterates while a condition is true.
  • As long as a variable returns true (e.g., isClosed), the loop continues.
  • The while loop stops executing once the variable returns false.
  • The do-while loop iterates as long as the specified variable is true.
  • do-while executes the loop at least once, then checks the condition.
  • If a variable returns false at any point, do-while stops executing.

Break and Continue Statements

  • Break - statement to stop loop
  • Continue - statement to skip to the next loop iteration

Switch and Case Statements

  • Can be used over a range of cases

Functions in Dart

  • Functions are used to group reusable logic.
  • Functions can take parameters and return values.
  • Since Dart is object-oriented, functions can be assigned to variables.
  • Functions can be passed as arguments to other functions.
  • For single-expression, use the arrow (=>) syntax.
  • All functions return a value by default.
  • If no return statement is specified, Dart adds a null return statement.
  • Expect a return type when calling functions.
  • Use the void type if no return is expected.
  • Specify data types when the function returns a value.

Example Usages of Functions

  • The app's main() function is an example of a function with void as the return type.
  • The function can take an int as a parameter.
  • When the code is executed, the print statement shows the value.
  • Since the function has a parameter, call it with the value orderEspresso(3).

Functions with Bool Values

  • Just after the function, a bool isOrderDone variable is initialized by calling the function and passing three;
  • The print statement then shows the bool value sent back.

Optional Function Parameters

  • Make function parameter optional by wrapping in square brackets ###Importing Packages
  • To use an external package, library, or class, use the import statement.
  • Separate code logic into different class files to make it understandable.
  • Only one argument is required, which is the uniform resource identifier (URI) of the class/library.
  • Specify the package: scheme before the URI if the library is created by a package manager.
  • To import a class, specify its location/class name or the package.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser