The generic

Generics are usually designed for type safety. Specifying generic types locally yields better code. Generics can also be used to reduce code duplication. Dart uses the local approach to define generics. For example, if you want a List to contain a String, you can declare it as List

. As follows:

void test(){
    var names = List<String> (); names.addAll(['cxl'.'zs'.'ls']);
}
Copy the code

For collection generics

Generics are used to parameterize List and Map types:

List:<type>
Map:<keyType,valueType>
Copy the code

Example:

var names = <String> ['cxl'.'zs'.'ls'];
var weeks = <String.String> {'Monday':'Monday'.'Tuesday':'Tuesday'.'Sunday':'Sunday'
}
Copy the code

Parameterize in the constructor

The Map type is as follows :var user = new Map

(); Var user = new List< user >();
,string>

Library to use

Reference library

Use import statements to reference files in one library from another. Note the following:

  • inimportThe statement needs to be followed by the library file path.
  • rightDartLanguage provides library files to usedart:xxFormat.
  • Third party library file usepackage:xxFormat.importHere is an example:
import 'dart:io';
import 'package:mylib/mylib.dart';
import 'package:utils/utils.dart';
Copy the code

Specifies the prefix of a library

When referencing libraries with conflicting names, you can specify different prefixes for one or more of them. This is close to the concept of a namespace, as shown in the following code:

import 'package:lib1/lib1.dart';
import 'package:lib1/lib1.dart' as lib2;

void test(){
    Element element1 = new Element(a);// Use Element in lib1
    lib2.Element element2=new lib2.Element(a);// Use Element in lib2
}
Copy the code

Dart and lib2/lib2.dart both contain Element classes. If you use lib1/lib1.dart, you don’t know which Element class is referenced.

Part of the reference library

If you only want to use part of the library, you can optionally use the following keywords:

  • showKey words: quote only a little.
  • hideKey words: in addition to all references.

Example code is as follows:

/ / import foo
import 'package:lib1/lib1.dart' show foo;
// Import everything except foo
import 'package:lib2/lib2.dart' hide foo;
Copy the code

Asynchronous support

The Dart language is one of the few languages currently supporting asynchronous operations. Async functions and await expressions are commonly used for asynchronous operations. The Dart library provides asynchronous functionality, which provides interfaces for time-consuming operations such as file reads and writes, and network requests. This function returns a Future or Stream object. You can obtain the Future object value returned by the Asynchronous function as follows:

  • useasyncFunctions andawaitExpression.
  • useFutureThe API provided by the function. You can obtain the value in the following waysasynchronousFunction return toStreamObject value:
  • useasyncAnd an asynchronous loop “await for”.
  • useStreamRelated API. The following example code is usedasyncorawaitAsynchronous processing, although the code looks like synchronous processing:
await readFile()
Copy the code

We must use await expressions in a function marked with the async keyword:

void fileOperate() async{
    // Read the file
    readFile()
    // Other operations
    / /...
}
Copy the code

Metadata (@)

Using yuan to add more information to the code, metadata starts with an @ modifier followed by a compile-time constant or calls a constant constructor. The Dart language currently provides three @ modifiers:

  • @deprecatedHave been abandoned.
  • @overrideRewrite.
  • @proxyThe agent.

annotation

Dart supports three annotation types

  • Single-line comments
  • Multiline comment
  • Documentation comments

Like JAVA