Dart4Flutter -01 — variables, types, and functions

Dart4Flutter — 02 — Control flow and anomalies

Dart4Flutter — 03 — Classes and generics

Dart4Flutter — 04 — async and library

Dart 4FLUTTER – gLEANer 01- Flutter- DART environment setup

class

Below is creating a class and creating an instance of the class.

main(List<String> args) {
  Dog d = new Dog();
}
 
class Dog {}Copy the code

Now we add instance variables and constructors to the class, and age and name attributes to the Dog class.

main(List<String> args) {
  Dog d = new Dog('Duffy'.2);
  print(d.name);
}
 
class Dog {
  String name;
  int age;
 
  Dog(String name, Stirng age) {
    this.name = name;
    this.age = age; }}Copy the code

Dart provides a more straightforward way to define constructors, as follows:

main(List<String> args) {
  Dog d = new Dog('Duffy'.2);
  print(d.name);
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
}
Copy the code

As shown above, we define the constructor on one line, where the first argument is copied to the name attribute and the second argument is assigned to the age attribute.

Named constructor

Dart provides another way to define functions: named constructors.

main(List<String> args) {
  Dog d = new Dog.newBorn();
  print(d.name);
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0; }}Copy the code

As shown above, the constructor is given a name, which makes the different constructors clearer.

Inheritance Inheritance

You can inherit from other classes using the extends keyword.

main(List<String> args) {
  Pug p = new Pug('Duffy'.5);
  print(p.name);
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0; }}class Pug extends Dog {
  Pug(String name, int age): super(name, age);
}
Copy the code

The Pug class inherits the Dog class and calls the Dog constructor with the super keyword.

You can also use the this keyword to call other constructors in the same class after the colon.

main(List<String> args) {
  Pug p = new Pug.small('Duffy');
  print(p.name);
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0; }}class Pug extends Dog {
  Pug(String name, int age): super(name, age);
 
  Pug.small(Stirng name): this(name, 1);
 
  Pug.large(Stirng name): this(name, 3);
}
Copy the code

As shown above, we define two named constructors that just need the name of dog, and then call the default constructor for Pug.

methods

Defining a method in a class is the same as defining a method independently.

main(List<String> args) {
  Dog d = new Dog('Duffy'.10);
  d.bark();
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0;
  }
 
  bark() {
    print('Bow Wow'); }}Copy the code

It’s as simple as rewriting the method.

main(List<String> args) {
  Pug p = new Pug.small('Duffy');
  p.bark();
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0;
  }
 
  bark() {
    print('Bow Wow'); }}class Pug extends Dog {
  Pug(String name, int age): super(name, age);
 
  Pug.small(Stirng name): this(name, 1);
 
  Pug.large(Stirng name): this(name, 3);
 
  @override
  bark() {
    print('Meow! '); }}Copy the code

Getter and Setters

By default, any variable in a fetching class can refer directly to the variable’s name. For example, dog.name, you can get or assign the value of name directly. But sometimes you want to customize the getter and setter methods for a property. You can customize the getter and setter methods using the GET and set keywords.

main(List<String> args) {
  Dog d = new Dog('Duffy'.5);
  d.respectedName = 'Mr.Duffy';
  print(d.respectedName);
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  String get respectedName {
    return 'Mr.$name';
  }
 
  set respectedName(String newName) {
    name = newName;
  }
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0;
  }
 
  bark() {
    print('Bow Wow'); }}Copy the code

Now, you might be thinking that the Name property is accessible and can be modified at will. So let me do that, privatize the name property.

Access control

All properties and methods in the default class are public. In DART, you can privatize properties and method names by adding “_” to them. Now let’s privatize the name attribute.

main(List<String> args) {
  Dog d = new Dog('Duffy'.5);
  print(d.name);  //This will throw error
}
 
class Dog {
  String _name;
  int age;
 
  Dog(this.name, this.age);
 
  String get respectedName {
    return 'Mr.$name';
  }
 
  set respectedName(String newName) {
    name = newName;
  }
 
  Dog.newBorn() {
    name = 'Doggy';
    age = 0;
  }
 
  bark() {
    print('Bow Wow');
  }
 
  _hiddenMethod() {
    print('I can only be called internally! '); }}Copy the code

Abstract classes and methods

You can declare abstract classes with the abstract keyword

abstract class AbstractDog {
  void bark();
 
  void _hiddenMethod();
}
Copy the code

You only need to add the abstract keyword before the class declaration, not the method. Methods need only be signed, not implemented.

A static method

If you want a method or property to be static, you simply add the static keyword before the declaration.

main(List<String> args) {
  Dog.bark(); 
}
 
class Dog {
  String name;
  int age;
 
  Dog(this.name, this.age);
 
  static bark() {
    print('Bow Wow'); }}Copy the code

The enumeration

Dart supports enumerations, which are used the same way as Java.

main(List<String> args) {
  Dog d = new Dog('Duffy'.12, CurrentState.sleeping);
  print(d.state == CurrentState.sleeping); //Prints 'true'
}
 
enum CurrentState {
  sleeping,
  barking,
  eating,
  walking
}
 
class Dog {
  String name;
  int age;
  CurrentState state;
 
  Dog(this.name, this.age, this.state);
 
  static bark() {
    print('Bow Wow'); }}Copy the code

The generic

Dart fully supports generics. Suppose you want to hold arbitrary types of data in a class that you define. Here’s how to define such a class using generics.

main(List<String> args) {
  DataHolder<String> dataHolder = new DataHolder('Some data');
  print(dataHolder.getData());
  dataHolder.setData('New Data');
  print(dataHolder.getData());
}
 
class DataHolder<T> {
  T data;
 
  DataHolder(this.data);
 
  getData() {
    return data;
  }
 
  setData(data) {
    this.data = data; }}Copy the code

The end of the

Dart4Flutter – 01 — variables, types, and functions

Dart4Flutter — 02 — Control flow and anomalies

Dart4Flutter — 03 — Classes and generics

Dart4Flutter — 04 — async and library

reference

Thetechnocafe.com/just-enough…