arrow_back

Dart: Variables, Data Structures, Objects, and Conditionals

Join Sign in

Dart: Variables, Data Structures, Objects, and Conditionals

1 hour 30 minutes 1 Credit

GSP1014

Google Cloud self-paced labs logo

Overview

Dart is a client-optimized language for developing fast apps on any platform. Its goal is to offer the most productive programming language for multi-platform development, paired with a flexible execution runtime platform for app frameworks.

Languages are defined by their technical envelope — the choices made during development that shape the capabilities and strengths of a language. Dart is designed for a technical envelope that is particularly suited to client development, prioritizing both development (sub-second stateful hot reload) and high-quality production experiences across a wide variety of compilation targets (web, mobile, and desktop).

Dart also forms the foundation of Flutter. Dart provides the language and runtimes that power Flutter apps, but Dart also supports many core developer tasks like formatting, analyzing, and testing code.

In this lab, you will learn the basics of Dart in a prepared development environment.

What you'll learn

  • Variables

  • Flow Control

  • Classes

Prerequisites

Based on the content, it is recommended to have some familiarity with:

  • General programming principles

Setup

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

Getting started

The lab environment includes an Editor and Browser pre-configured for Dart.

  1. Access these resources using the lab credentials panel.

  2. Copy the IDE link and paste it into a new browser.

Task 1. Flutter repository

In the code editor clone the flutter repository and then access the relevant code sample.

  1. In the editor select Source Control.

Source Control selected in the editor, which includes two buttons; Open Folder and Clone Repository.

  1. Select Clone Repository.
  2. Enter the following repository:
https://github.com/rosera/flutter_workshop.git
  1. Clone to the default directory.
Note: As the repository is cloned, the editor will raise helpful notifications. These are not required for this lab. Alternatively you may use the editor available at dart.dev.

With the Repository now cloned onto your environment the Flutter Workshop repository is now available. For this lab, use the dart folder to complete the exercises.

  1. Select the dart folder

A solution folder containing working examples is also available in the same repository:

Solution Directory Contents
dart/lab01/solutions Variables
dart/lab02/solutions Flow control
dart/lab04/solutions Import statements
dart/lab05/solutions Classes
Note: Please refer to the correct directory if you need assistance with a solution.

Task 2. Introduction to Dart

To get an overview of what Dart is and why it is important, watch the Introduction to Dart video.

In addition, the following guidelines on Effective Dart will be useful:

  • Style Guide – This defines the rules for laying out and organizing code, or at least the parts that dart format doesn’t handle for you. The style guide also specifies how identifiers are formatted: camelCase, using_underscores, etc.

  • Documentation Guide – This tells you everything you need to know about what goes inside comments. Both doc comments and regular, run-of-the-mill code comments.

  • Usage Guide – This teaches you how to make the best use of language features to implement behavior. If it’s in a statement or expression, it’s covered here.

  • Design Guide – This is the softest guide, but the one with the widest scope. It covers what we’ve learned about designing consistent, usable APIs for libraries. If it’s in a type signature or declaration, this goes over it.

Dart: Hello World

To get started with Dart, write a traditional Hello World program. Create an application based on the traditional Hello World code.

  1. Create new file hello-world.dart.
  2. Add the following code:
void main(){ print('Hello World!'); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Hello World! displayed on the Debug Console tabbed page

Task 3. Variables

In this section, learn how to declare different types of variables with Dart.

Learn about the following variables types and their use in Dart:

  • Const

  • Final

  • Lists

  • Maps

Dart: Hello Const

Create an application to use Const.

  1. Create new file hello-const.dart.
  2. Add the following code:
void main(){ const int daysInWeek = 7; print ('Hello $daysInWeek'); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Dart: Hello Final

Create an application to use Final.

  1. Create new file hello-final.dart.
  2. Add the following code:
void main(){ final int numberOfWorkDays = 5; print ('Hello $numberOfWorkDays'); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Dart: Hello List

Create an application to use Lists.

  1. Create new file hello-list.dart.
  2. Add the following code:
void main(){ List<String> namesOfTheWeek = ['mon', 'tues', 'wed', 'thurs', 'fri']; print ('Hello $namesOfTheWeek'); print ('Hello $namesOfTheWeek[0]'); print ('Hello $namesOfTheWeek[${namesOfTheWeek.length}]'); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Note: The output from the program displays the following information:
  • Print the items in the list
  • Print the first element in the list
  • Print the number of elements found in the list

Dart: Hello Maps

Create an application to use Maps.

  1. Create new file hello-map.dart.
  2. Add the following code:
void main(){ var daysInWeek = new Map(); daysInWeek[0] = 'Sun'; daysInWeek[1] = 'Mon'; daysInWeek[2] = 'Tues'; print('Hello $daysInWeek'); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Task 4. Flow Control

In this section learn how to apply conditional logic in Dart.

For statement

Create an For statement.

  1. Create new file hello-for.dart.
  2. Add the following code:
void main() { String myMessage = 'Dart is super fun!'; for (int i = 0; i < 5; i++){ print('Hello $myMessage'); } }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

While statement

Create an While statement.

  1. Create new file hello-while.dart.
  2. Add the following code:
void main() { bool loopFlag = true; int i = 0; while (loopFlag) { if (i <= 4){ print('Hello $i'); i++; } else { loopFlag = false; } } }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Task 5. Classes

In this section learn how to declare a Class and use them in Dart.

At a high level we need to know about the following:

  • Dart is an object oriented language

  • Classes establish an objects functionality

  • Objects have functions and data (i.e. members)

  • Classes use constructors to initialize settings

Class without parameters

Create an application to use functions without parameters.

  1. Create new file hello-function.dart.
  2. Add the following code:
class FlutterDev { FlutterDev() { print('I am a Flutter Developer'); } } void main() { var typeOfDeveloper = new FlutterDev(); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Class with parameters

Create an application to use functions with parameters.

  1. Create new file hello-function2.dart.
  2. Add the following code:
class FlutterDev { FlutterDev(String name) { print('I am $name a Flutter Developer'); } } void main() { var typeOfDeveloper = new FlutterDev('Jamie'); }
  1. Save the code.
Note: Once the code is saved, a Run|Debug menu option will appear.
  1. Select the Run option to execute the code.

The program output will be displayed in the debug console.

Awesome work getting started with Dart.

Click Check my progress to verify the objective. Assess my progress

Congratulations!

You have successfully completed the lab and demonstrated your knowledge of Dart. Over the course of this lab, you been introduced to the following Dart fundamentals:

  • Variables

  • Flow Control

  • Classes

Next steps / Learn more

  • Flutter Introduction
  • Flutter Intermediate

Manual Last Updated September 19, 2022

Lab Last Tested February 8, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.