Flutter tutorial: Getting start with Dart

Dart is created by Google in 2011, and can be use for web, server, mobile, and IoT, also can be use to develop Flutter.

Here we will present a quickly short guid for let you understand what is Dart and how to use it. For more detail about Dart can reference tour of basic Dart program.

Installation the Dart SDK

Using brew to install Dart in mac by excute following command, using brew tap dart/lang-dart and installing dart from dart-lang/dart:

brew tap dart/lang-dart
brew install dart

Open VSCode and install plugins for dart development.

Create a index.dart and input code like this:

void main() {
	print('Hello World Dart');
}

Press Control+Option+N to Run code.

[Running] dart "/Users/me/project/dart/index.dart"
Hello World Dart

Variable

About variable can not specific type using var, or specific type like String, int

Okay~ here you see the variable var like javascript ~ yes it is 😂.

Following showing how declare variable and print out:

void main(){
  var name = "Adam";
  String info = "writing Dart";
  int years = 2021;
  double number = 1.234;
  print("Hello World Dart $name $info in $years, show number: $number");
}

Output:

Hello World Dart Adam writing Dart in 2021, show number: 1.234

Const

For declare the const in Dart can using final and const, the different is:

  • final can set only once
  • const is compile-time constant, and finally be final

When using const, can ignore type:

final user = 'Adam';
final String language = 'CN-TW';
const description = 'Hi!';
const double amount = 1.234;

If declare type for final, we can’t change the type again.

If don’t decare any prefix like following, the variable will using const as default value:

name = 'Adam';

Lists, Sets and Maps

Dart is represent array as form of List.

About Set and Map are collection, and the Map is a key/value formate.

Here is a example:

void main(){
  var usersList = ['Adam', 'Brown', 'Cayla', 'Devin'];
  var usersSet = {'Adam', 'Brown', 'Cayla', 'Devin'};
  var usersMap =  {
    'name': 'Adam',
    'age': 10,
    'title': 'Student',
    'Amount': 1.234
  };

  //Lists loop
  print('---------\n Lists Loop:');
  for (var row in usersList) {
    print(row);
  }

  //Sets Loop
  print('---------\n Sets Loop:');
  for (var row in usersSet) {
    print(row);
  }

  //Maps Loop
  print('---------\n Maps Loop:');
  usersMap.forEach((k,v) => print('$k and $v'));
}

Output:

---------
 Lists Loop:
Adam
Brown
Cayla
Devin
---------
 Sets Loop:
Adam
Brown
Cayla
Devin
---------
 Maps Loop:
name and Adam
age and 10
title and Student
Amount and 1.23

Functions and Lambda function

In Dart, function is don’t need declare function, just like following formate:

functionName() {
   ...
}

Lambda function (also call arrow function) is another way for declare function:

String userData => 'hello';

For example:

void main(){
 var name = getUserName();
 var age = getAge();
 print(' name: $name, and age is $age');
}

//Function
getUserName() {
  return 'Adam';
}

//Lambda Functions (or Arrow functions)
getAge() => 18;

Output:

 name: Adam, and age is 18

Class

Dart class can declare with class :

class className {  
   <fields> 
   <getters/setters> 
   <constructors> 
   <functions> 
}

Access the class can using new className :

var objectName = new className([ art ])

For example:

void main (){
  UserData user = new UserData('Adam');
  print(user.userName);

  user.userName = 'Brown';
  print(user.userName);

  int number = 3;
  var results = user.caculateor(number);
  print(results);
}

//Class
class UserData {
  //Fields
  String name = '';

  //Constructors
  UserData(String defaultName) { 
    this.name = defaultName;
  } 

  //Function
  caculateor(int n) {
    return n*n;
  }
  
  // Setter
  void set userName(String name) { 
    //this is mean current instance of the class.
    this.name = name; 
  } 

  //Getter
  String get userName {
    return this.name;
  } 
}

Output:

Adam
Brown
9

Interface

In Dart, the class can implement more Interface at one times:

class className implements interface1,interface2,interface3..

If we implement the interface, will need implementation for those members.

For example:

void main (){
  UserData user = new UserData('Adam');
  print(user.userName);

  user.userName = 'Brown';
  print(user.userName);

  int number = 3;
  var results = user.caculateor(number);
  print(results);
}

class UserInterface {
  int caculateor(int n) { return n; }
}

class UserData implements UserInterface{
  //Fields
  String name = '';

  //Constructors
  UserData(String defaultName) { 
    this.name = defaultName;
  } 

  //Functions
  int caculateor(int n) {
    return n*n;
  }
  
  // Setter
  void set userName(String name) { 
    //this is mean current instance of the class.
    this.name = name; 
  } 

  //Getter
  String get userName {
    return this.name;
  } 
}

If remove caculator in UserData class, the error will show:

index.dart:14:7: Error: The non-abstract class 'UserData' is missing implementations for these members:
 - UserInterface.caculateor
Try to either
 - provide an implementation,
 - inherit an implementation from a superclass or mixin,
 - mark the class as abstract, or
 - provide a 'noSuchMethod' implementation.

class UserData implements UserInterface{
      ^^^^^^^^
index.dart:11:7: Context: 'UserInterface.caculateor' is defined here.
  int caculateor(int n) { return n; }
      ^^^^^^^^^^

How declare library and import?

Dart can declare a library and import, the library file format like this:

library libraryName;

//contents

For example, here we generate a file userLibrary.dart

userLibrary.dart

library userLib;

class UserInterface {
  int caculateor(int n) { return n; }
}

class UserData implements UserInterface{
  //Fields
  String name = '';

  //Constructors
  UserData(String defaultName) { 
    this.name = defaultName;
  } 

  //Functions
  int caculateor(int n) {
    return n*n;
  }
  
  // Setter
  void set userName(String name) { 
    //this is mean current instance of the class.
    this.name = name; 
  } 

  //Getter
  String get userName {
    return this.name;
  } 
}

Here just import the file:

import 'userLibrary.dart';

void main (){
  UserData user = new UserData('Adam');
  print(user.userName);

  user.userName = 'Brown';
  print(user.userName);

  int number = 3;
  var results = user.caculateor(number);
  print(results);
}

Also, you can import as nick name:

import 'userLibrary.dart' as userLib;

void main (){
  userLib.UserData user = new userLib.UserData('Adam');
  print(user.userName);

  user.userName = 'Brown';
  print(user.userName);

  int number = 3;
  var results = user.caculateor(number);
  print(results);
}