1. Data types

Boolean

void main(){
  bool isTrue = true;
  bool isFalse = false;

  print("Hello".isEmpty);
}
Copy the code

Number

void main(){ num a = 10; A = 12.5; int b = 20; / / b = 20.5; Double c = 10.5; // c = 30;print(b + c);
  print(b - c);
  print(b * c);
  print(b / c);
  print(b ~/ c);
  print(b % c);

  print(0.0/0.0);print(b.isEven);
  print(b.isOdd);
  int d = 11;
  print(d.isEven);
  print(d.isOdd);

  int e = -100;
  print(e.abs());

  double f = 10.5;
  print(f.round());
  print(f.floor());
  print(f.ceil());

  print(f.toInt());
  print(d.toDouble());

}

Copy the code

String

void main(){
  String str1 = 'Hello'; //""
  String str2 = ' ''Hello Dart'' '; //""" print(str2); // String str3 = 'Hello \n Dart'; String str3 = r'Hello \n Dart'; print(str3); String str4 = "This is my favorite language"; print(str4 + "New"); print(str4 * 5); print(str3 == str4); print(str4[1]); int a = 1; int b = 2; print("a + b = ${a + b}"); print("a = $a"); print(str4.length); print(str4.isEmpty); print(str4.contains("This"));
  print(str4.substring(0,3));
  print(str4.startsWith("a")); print(str4.endsWith("ge")); var list = str4.split(" "); print(list); print(str4.replaceAll("This","That"));
}

Copy the code

Const

void main(){
  var a;
  print(a);

  a = 10;
  print(a);

  a = 'Hello Dart';
  print(a);

  var b = 20;
  print(b);

  final c = 30;
//  c = 50;

  const d = 20;
//  d = 50;

}
Copy the code

List

void mainVar list1 = [1,2,3,"Dart".true];
  print(list1);
  print(list1[2]);
  list1[1] = "Hello";
  print(list1); Var list2 = const [1,2,3]; // list2[0] = 5; var list3 = new List(); var list = ["hello"."dart"];
  print(list.length);
  list.add("New");
  print(list);

  list.insert(1, "Java");
  print(list);

  list.remove("Java");
  print(list);

//  list.clear();
//  print(list);

  print(list.indexOf("dart1"));
  list.sort();
  print(list);

  print(list.sublist(1));

  list.forEach(print);
}

Copy the code

Map

void main(){
  var map1 = {"first":"Dart", 1:true.true:"2"};
  print(map1);

  print(map1["first"]);
  print(map1[true]);
  map1[1] = false;
  print(map1);

  var map2 = const {1:"Dart", 2:"Java"}; // immutable // map2[1] ="Python";

  var map3 = new Map();

  var map = {"first":"Dart"."second":"Java"."third":"Python"};
  print(map.length);
//  map.isEmpty;

  print(map.keys);
  print(map.values);

  print(map.containsKey("first"));
  print(map.containsValue("C"));

  map.remove("third");
  print(map);

  map.forEach(f);

  var list = ["1"."2"."3"];
  print(list.asMap());
}

void f(key,value){
  print("key=$key,value=$value");
}

Copy the code

dynamic

void main(){
  var a;
  a = 10;
  a = "Dart";

  dynamic b = 20;
  b = "JavaScript";

  var list = new List<dynamic>();
  list.add(1);
  list.add("hello");
  list.add(true);
  print(list);
}

Copy the code

2. The operator

operator_arithmetic

void main(){
  int a = 10;
  int b = 2;

  print(a + b);
  print(a - b);
  print(a * b);
  print(a / b);
  print(a ~/ b);
  print(a % b);

  print(a++); / / 11print(++a); / / 12print(a--); / / 11print(--a); / / 10}Copy the code

operator_asignment

void main(){ int a = 10; int b = 5; b ?? = 10;print(b);

  a += 2;
  print(a);

  a -= b;
  print(a);

  a *= b;
//  a /= b;
  a ~/= b;
  a %= b;

}
Copy the code

operator_conditional

void main(){
  int gender = 1;
  String str = gender == 0 ? "Male=$gender" :"Female=$gender";
  print(str);

  String a = "Dart";
  String b = "Java";
  String c = a ?? b;
  print(c);


}

Copy the code

operator_logical

void main(){
  bool isTrue = true;
  print(! isTrue); bool isFalse =false;
  print(isTrue && isFalse);
  print(isTrue || isFalse);

  String str = "";
  print(! str.isEmpty); }Copy the code

operator_relational

void main(){
  int a = 5;
  int b = 3;

  print(a == b);
  print(a ! =b );print(a > b);
  print(a < b);
  print(a >= b);
  print(a <= b);

  String strA = "123";
  String strB = "123";
  print(strA == strB);
}

Copy the code

3. Control statements

break_and_continue

void main(){var list = [1,2,3];for(var item in list){
    if(item == 2) {
//      break;
      continue;
    }
    print(item);
  }

  print("-- -- -- -- --"); Var list2 = (4 and 6);for(var item1 in list){
    if(item1 == 2){
      break;
    }

    for(var item2 in list2){
      if(item2 == 5){
        break;
      }
      print(item2); }}}Copy the code

stmt_assert

void main(){ String str; assert(str ! = null); }Copy the code

stmt_for

void mainVar list = [1,2,3,4,5];for(var index = 0; index < list.length; index++){print(list[index]);
  }

  print("-- -- -- -- -- -- -- -- --");
  for(var item in list){
    print(item); }}Copy the code

stmt_if

void main(){
  int score = 100;

  if(score >= 90){
    if(score == 100){
      print("Perfect");
    }else {
      print("Good"); }}else if(score > 60){
    print("Good");
  }else if(score == 60){
    print("Pass");
  }else {
    print("Fail"); }}Copy the code

stmt_switch

void main(){
  String language = "Java";

  switch(language){
    case "Dart":
      print("Dart is my favorite");
      break;
    case "Java":
      print("Java is my favorite");
      break;
    case "Python":
      print("Python is my favorite");
      break;
    default:
      print("None");
  }

  switch(language){
    Test:
    case "Dart":
      print("Dart is my favorite");
      break;
    case "Java":
      print("Java is my favorite");
      continue Test;
//      break;
    case "Python":
      print("Python is my favorite");
      break;
    default:
      print("None"); }}Copy the code

stmt_while

void main(){
  int count = 0;

  while(count < 5){
    print(count++);
  }

  print("-- -- --$count-");

  do{
    print(count--);
  }while(count > 0 && count < 5);


}
Copy the code

Method 4.

function_declaration

void main(List args){
  print(args);

  print(getPerson("Zhang", 18));

  print(printPerson("Bill", 20));
}

int gender = 1;
getPerson(name,age) => gender == 1 ? "name=$name,age=$age":"Test";

printPerson(name,age){
  print("name=$name,age=$age");
}

Copy the code

Function_optional_param This parameter is optional

void main() {printPerson("Bill");
  printPerson("Bill",age: 20);
  printPerson("Bill",age: 20,gender: "Male");
  printPerson("Bill",gender: "Male");

  printPerson2("Zhang");
  printPerson2("Zhang", 18);printPerson2("Zhang", 18."Female");
}

printPerson(String name,{int age,String gender}){
  print("name=$name,age=$age,gender=$gender");
}

printPerson2(String name,[int age,String gender]){
  print("name=$name,age=$age,gender=$gender");
}

Copy the code

Function_default_param Default parameter

void main() {printPerson("Bill");
  printPerson("Bill",age: 20);
  printPerson("Bill",gender: "Male");
}

printPerson(String name,{int age = 30,String gender = "Female"{})print("name=$name,age=$age,gender=$gender");
}
Copy the code

Function_object Method object

void main(){
  Function func = printHello; func(); Var list = [1, 2, 3, 4]. list.forEach(print);

  var list2 = ["h"."e"."l"."l"."o"];
  print(listTimes(list2, times));
}

void printHello() {print("Hello");
}

List listTimes(List list ,String times(str)){
  for(var index = 0; index < list.length; index++){ list[index] =times(list[index]);
  }

  return list;
}


String times(str){
  return str*3;
}


Copy the code

Function_anonymous Anonymous method

void main(){

  var func = (str){
    print("Hello---$str"); }; func(30); / / (() {/ /print("Test"); / /}) (); var list2 = ["h"."e"."l"."l"."o"];

  var result = listTimes(list2, (str){ returnstr * 3; });print(result);

  print(listTimes2(list2));

}

List listTimes(List list ,String times(str)){
  for(var index = 0; index < list.length; index++){ list[index] =times(list[index]);
  }

  return list;
}

List listTimes2(List list ){
  var func = (str){ returnstr * 3; };for(var index = 0; index < list.length; index++){ list[index] = func(list[index]); }return list;
}

Copy the code

Function_closure closure

void main(){
  var func = a();
  func();
  func();
  func();
  func();

}

a(){
  int count = 0;

//  printCount() {/ /print(count++);
//  }

  return() {print(count++);
  };
}

Copy the code

5. Object oriented

Class_and_object Classes and objects

import 'person.dart';

void main() {
    var person = new Person();
    person.name = "Tom";
    person.age = 20;

    print(person.name);
    print(person.address);

    person.work();
}
Copy the code
//person.dart
class Person {
  String name;
  int age;
  final String address = "";

  void work() {print("Name is $name,Age is $age,He is working..."); }}Copy the code

Calculated_property Computes attributes

void main() {
    var rect = new Rectangle();
    rect.height = 20;
    rect.width = 10;

    print(rect.area);

    rect.area = 200;
    print(rect.width);

}

class Rectangle{
  num width,height;

  num get area => width * height;
      setarea(value){ width = value / 20; }}Copy the code

Constructors methods of construction

void main() {
    var person = new Person("Tom", 20,"Male");
    new Person.withName("John");
    new Person.withAge(20);
}

class Person{
  String name;
  int age;

  final String gender;

  Person(this.name,this.age,this.gender);

  Person.withName(String name){
    this.name = name;
  }

  Person.withAge(this.age);

  void work() {print("Work..."); }}Copy the code

Const_constructors Constant construction method

void main() {
    const person = const Person("Tom", 20,"Male");

    person.work();

}

class Person{
  final String name;
  final int age;

  final String gender;

  const Person(this.name,this.age,this.gender);

  void work() {print("Work..."); }}Copy the code

Factory_constructors factory construction method

class Logger {
  final String name;

  static final Map<String, Logger> _cache = <String, Logger>{};

  factory Logger(String name) {
    if (_cache.containsKey(name)) {
      return _cache[name];
    } else {
      final logger = Logger._internal(name);
      _cache[name] = logger;
      return logger;
    }
  }

  Logger._internal(this.name);

  void log(String msg) {
    print(msg); }}Copy the code

Init_list Initializes the list

void main() {
    var person = new Person("Tom", 20, "Male");
}

class Person{
  String name;
  int age;

  final String gender;

  Person(this.name,this.age,this.gender);

  Person.withMap(Map map): name = map["name"],gender = map["gender"]{
    age = map["age"];
  }

  void work() {print("Work..."); }}Copy the code

Static_member Static member

void main() { var page = new Page(); Page.scrollDown(); } class Page{ static const int maxPage = 10; static int currentPage = 1; // Drop static voidscrollDown(){
    currentPage = 1;
    print("ScrollDown..."); } // slide up voidscrollUp(){
    currentPage++;
    print("scrollUp..."); }}Copy the code

Object_operator Object operator

void main() { // Person person = new Person(); // person? .name; // var person; // person ="";
//  person = new Person();

//  (person as Person).work();

//  if(person is Person){ // person.work(); // } new Person() .. name ="Tom". age = 20 .. work(); } class Person{ String name; int age; voidwork() {print("Work...$name.$age"); }}Copy the code

Object_call object call

void main() {
    var person = new Person();

    print(person("Test", 30)); } class Person{ String name; int age; // voidwork() {/ /print("Name is $name,Age is $age");
//  }

  String call(String name,int age){
    return "Name is $name,Age is $age"; }}Copy the code

6. Object-oriented extension

inheritance

import 'person.dart';

void main() {
//  var student = new Student();
//  student.study();
//
//  student.name = "Tom";
//  student.age = 16;
//
//  print(student.isAdult);
//
//  student.run();

  Person person = new Student();
  person.name = "Tom";
  person.age = 18;

  if(person is Student){
    person.study();
  }

  print(person);
}

class Student extends Person{

  void study() {print("Student study...");
  }

  @override
  bool get isAdult => age > 15;


  @override
  void run() {
    print("Student run..."); }}Copy the code

class Person {
  String name;
  int age;
  String _birthday;

  bool get isAdult => age > 18;

  void run() {print("Person run...");
  }

  @override
  String toString() {
    return "Name is $name,Age is $age"; }}Copy the code

Constructor in inheritance


void main() {
  var student = new Student("Tom"."Male");

  print(student.name);
}

class Person{
  String name;

  Person(this.name);

  Person.withName(this.name);
}

class Student extends Person{
  int age;

  final String gender;

  Student(String name,String g) : gender = g, super.withName(name);
}

Copy the code

An abstract class

void main() {
  var person = new Student();
  person.run();
}

abstract class Person{

  void run();
}

class Student extends Person{
  @override
  void run() {
    print("run..."); }}Copy the code

interface

void main() {
  var student = new Student();
  student.run();
}

//class Person{
//  String name;
//
//  int get age => 18;
//
//  void run() {/ /print("Person run...");
//  }
//}

abstract class Person{

  void run();

}

class Student implements Person{

  @override
  void run() {
    print("Student run..."); }}Copy the code

mixins

void main() {
//  var d = new D();
//  d.a();
}

//class A{
//  void a() {/ /print("A.a()...");
//  }
//}
//
//class B{
//
//  void a() {/ /print("B.a()...");
//  }
//
//  void b() {/ /print("B.b()...");
//  }
//}
//
//class Test{}
//
//
//class C{
//
//  void a() {/ /print("C.a()...");
//  }
//
//  void b() {/ /print("C.b()...");
//  }
//
//  void c() {/ /print("C.c()...");
//  }
//}
//
//class D  extends A with C,B{
//
//}




abstract class Engine{
  void work();
}

class OilEngine implements Engine{
  @override
  void work() {
    print("Work with oil...");
  }

}

class ElectricEngine implements Engine{

  @override
  void work() {
    print("Work with Electric...");
  }

}

class Tyre{
  String name;

  void run(){}
}

class Car = Tyre with ElectricEngine;

class Bus = Tyre with OilEngine;




Copy the code

Overwrites the operator

void main() {

  var person1 = new Person(20);
  var person2 = new Person(20);

  print(person1 > person2);

  person1.age;
  print(person1['age']);

  print(person1 == person2);
}

class Person{
  int age;

  Person(this.age);

  bool operator >(Person person){
    return this.age > person.age;
  }

  int operator [](String str){
    if("age" == str){
      return age;
    }

    return 0;
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is Person &&
              runtimeType == other.runtimeType &&
              age == other.age;

  @override
  int get hashCode => age.hashCode;


}

Copy the code

7. The enumeration

enum

void main() {
  var currentSeason  = Season.winter;

  print(currentSeason.index);

  switch(currentSeason){
    case Season.spring:
      print("1 - march");
      break;
    case Season.summer:
      print("June 4 -");
      break;
    case Season.autumn:
      print("September 7 -");
      break;
    case Season.winter:
      print("December 10 -");
      break;
  }
}

enum Season{
  spring,
  summer,
  autumn,
  winter
}

Copy the code

8. Generics

generics

void main() {
  var list = new List<int>();
  list.add(1);

//  var utils = new Utils<int>();
//  utils.put(1);

  var utils = new Utils();
  utils.put<int>(1);
}

class Utils{

   void put<T>(T element){
    print(element); } // void putString(String element){ // this.elementStr = element; / /}}Copy the code