“This is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Introduction to the

Those of you familiar with JAVA may know that the concept of generics was introduced in 8. What are generics? Generics are a generic type format commonly used in collections to specify which object formats should be stored in that collection.

Generics can simplify our programming and reduce errors, which is very convenient.

Generics are also available in the DART language. Take a look.

Why generics

The main purpose of using generics is to be type safe. For example, if we have a List and only want String in the List, we can specify this in DART:

var stringList = <String>[]; stringList.addAll(['jack ma', 'tony ma']); stringList.add(18); / / an errorCopy the code

You can only add strings to a stringList, and if you add numbers to it, an error is reported to ensure that the types in the List are consistent.

Clever use of generics can also reduce the amount of code we need because generics can represent a common class of types.

For example, in school, we have dormitories, which are divided into men and women, so there is a definition for boys:

abstract class BoyRoom {
    Boy getByName(String name);
}
Copy the code

For girls, there’s this definition:

abstract class GirlRoom{
    Girl getByname(String name);
}
Copy the code

In fact, the two are essentially the same, except that the type of the argument or return value has changed, so we can write:

abstract class Room<T>{
    T getByname(String name);
}
Copy the code

This simplifies the use of code.

How do I use generics

Generics are usually represented by uppercase single characters, typically E, T, S, K, and V.

The most common use of generics is in collections, such as lists, sets, and maps:

var listExample = <String>['jack ma', 'tony ma'];
var setExamples = <String>{'jack ma', 'tony ma'};
var mapExamples = <String, String>{
  'name1': 'jack ma',
  'name2': 'tony ma',
};
Copy the code

Generics can also be used in the constructors of these collection classes, as follows:

var stringMap = Map<String, String>();
Copy the code

Indicates that the constructed collection should contain the corresponding type.

Type erasure

Although generics are also available in JAVA, generics in JAVA have a type erasure feature. When do types erase? Type erasure is the type specified by a generic type. It only takes effect at compile time, and there is no concept of generics at run time.

For a List, JAVA can only determine at runtime whether the object is a List, not whether the object is a List.

Dart, unlike Java, can carry type information at run time. That is, dart can determine if an object is a List.

var stringList = <String>[];
stringList.addAll(['jack ma', 'tony ma']);
print(names is List<String>); // true
Copy the code

Inheritance of generics

The purpose of using generics is to limit the types of parameters, so we usually specify the parent class of the generic to limit the type range of the generic:

class Room<T extends Student> { } class Boy extends Student {... }Copy the code

We can pass Student itself or Boy, a subclass of Student, or not pass:

var student = Room<Student>();
var boy = Room<Boy>();
var studentDefault = Room();
Copy the code

Generic method

Generics in DART can be used in methods as well as classes:

T doSomething<T>(List<T> list) {
  T result = list[0];
  return result;
}
Copy the code

Generics specified in methods can be used in return types, parameters, and local variable types in methods.

conclusion

That’s an introduction to generics and their use in DART.

This article is available at www.flydean.com/08-dart-gen…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!