Tutorials
Dart

Basics of Dart and Flutter tutorials

What is Dart Language?

Dart is type-safe; it employs static type checking to ensure that a variable’s value always matches the static type of the variable. This is sometimes referred to as sound typing. Although types are required, type annotations are not required due to type inference. Dart’s typing system is also adaptable, allowing the usage of a dynamic type in conjunction with runtime checks, which might be beneficial during experimentation or for work that requires a high degree of dynamicity.

Dart has good null safety, which means that values can’t be null until you explicitly state they can. Dart can protect you from null exceptions at runtime using good null safety and static code analysis. Unlike many other null-safe languages, when Dart concludes that a variable is non-nullable, it stays that way. If you examine your running code in the debugger, you’ll notice that non-nullability is preserved at runtime (hence sound null safety).

Several Dart language features are demonstrated in the following code sample, including libraries, async calls, nullable and non-nullable types, arrow syntax, generators, streams, and getters. See the samples page for examples of how to use other Dart capabilities. Take the Dart language tour to discover more about the language.

Doc: For More Dart Examples

Simple Hello Program

Description: To display text on the console print() is used.

Code:

void main() {

  for (int i = 0; i < 5; i++) {

    print(‘hello ${i + 1}’);

  }

}

Output:

hello 0

hello 1

hello 2

hello 3

Types of Variable

Dart variables, like variables in other languages, have an associated data type (C, Python, Java).

A variable’s data type specifies the following:

  • The quantity of space available
  • Possible outcomes
  • The variable operation to be performed

Dart is a statically typed programming language, which means that variables always have the same type and cannot be modified.

The following data types are used in Dart programming:

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Maps

Code:

void main() {

  int age = 30;

  print (age);

  String name = “Lee Yu”;

  print (name);

  bool isNight = true;

  print(isNight);

  dynamic d1 = “String”;

  d1 = 43;

  print(d1);

var year = 1995;

var car = “Ferrari F40”

}

Output:

30

Lee Yu

true

43

Type Method

Description: It’s recommended to specify the arguments and return type of functions. Shortened => (arrow) syntax is very useful when needed to return a single statement.

Code:

void main() {

  print(‘something’);

  String greet = greeting();

  int age = getAge();

  String name = getName();

  print(greet);

  print(name);

  print (age);

}

String greeting(){

  return ‘hello’;

}

int getAge(){

  return 30;

}

String getName() => ‘Leo Yu’;

Output:

something

hello

Leo Yu

30

List and its methods

Description: The list is a collection of values. When List<type> is used only that type of value is accepted in the list. On List, you can perform operations using the following methods: add(), and remove().

Code:

void main() {

  List list = [‘Rock’,’Cena’,’Leo’,30];

  list.add(‘Brock’);

  list.remove(‘Cena’);

  print(list);

  List<int> num =[32,25,10,54];

  print(num);

}

Output:

[Rock, Leo, 30, Brock]

[32, 25, 10, 54]

Class and Object/Instance of Class

Description: Dart classes are the blueprint of the object, or they can be called object constructors. A class can contain fields, functions, constructors, etc. It is a wrapper that binds/encapsulates the data and functions together; that can be accessed by creating an object

Code:

void main() {

User userOne = User(‘Cena’,35);

  print(userOne.username);

  print(userOne.age);

User userTwo = User(‘Mario’,25);

  print(userTwo.username);

  print(userTwo.age);

  userTwo.login();

}

class User{

  String username = ”;

  int age = 0;

  User(this.username, this.age);

  void login(){

    print(‘user logged in’);

  }

}

Output:

Cena

35

Mario

25

user logged in

Class Inheritance

Description: Dart inheritance is defined as the process of deriving the properties and characteristics of another class.

Dart allows one class to inherit another, allowing it to generate a new class from an existing one. To accomplish this, we employ the extend keyword.

Terminology:

  • The parent class is the one whose properties the child class inherits. It is sometimes referred to as a base class or superclass.
  • The class that inherits the properties of the other classes is known as a child class. It’s also referred to as a deprived class or subclass.

Code:

void main() {

User userOne = User(‘Cena’,35);

  print(userOne.username);

  print(userOne.age);

User userTwo = User(‘Mario’,25);

  print(userTwo.username);

  print(userTwo.age);

  userTwo.login();

  SuperUser userThree = SuperUser(‘Lato’,18);

  print(userThree.username);

  userThree.publish();

  userThree.login();

}

class User{

  String username = ”;

  int age = 0;

  User(this.username, this.age);

  void login(){

    print(‘user logged in’);

  }

}

class SuperUser extends User{

  SuperUser(String username,int age) : super(username,age);

  void publish(){

    print(‘published update’);

  }

}

Output:

Cena

35

Mario

25

user logged in

Lato

published update

user logged in

Flutter

System Requirements

To install and run Flutter, your development environment must meet these minimum requirements:

  • Operating Systems: Windows 10 or later (64-bit), x86-64 based.
  • Disk Space: 1.64 GB (does not include disk space for IDE/tools).
  • Tools: Flutter depends on these tools being available in your environment.
    • Git for Windows 2.x, with the Use Git from the Windows Command Prompt option.
      If Git for Windows is already installed, make sure you can run git commands from the command prompt or PowerShell.
    • Android Studio or VS Code Editor

Get Flutter SDK

Clone the repo

C:\src>git clone https://github.com/flutter/flutter.git -b stable

Navigate to the cloned folder and run flutter_console.bat file

After Command Window open, Run C:\src\flutter>flutter doctor

[-] Android toolchain – develop for Android devices

    • Android SDK at D:\Android\sdk

    ✗ Android SDK is missing command line tools; download from https://goo.gl/XxQghQ

    • Try re-installing or updating your Android SDK,

      visit https://docs.flutter.dev/setup/#android-setup for detailed instructions.

Run this command to config android studio licenses

C:\src\flutter> flutter doctor –android-licenses

Type ‘y’ to accept all terms.

Next, Create a Virtual Device in Android Studio.

Leave a comment

Your email address will not be published. Required fields are marked *