The online programming

DartPad

Dartpad. Cn /? Null_safet…

Configuration related

Enable the Linter rule

The rules for static analysis can be configured by adding a file named Analysis_options. yaml to the root path of the Flutter project

For details, see github.com/jonataslaw/…

Details: dart.cn/guides/lang…

Coding related

Air escape operator

Dart provides a convenient set of operators for handling variables that might be null. One of them is…? = the assignment operator that assigns to the variable only if it is null:

int? a; // = nulla ?? =3;
print(a); // <-- Prints 3.a ?? =5;
print(a); // <-- Still prints 3.
Copy the code

The other air-avoidance operator is?? If the expression to the left of the operator returns a null value, the expression to the right is evaluated and returned.

print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
Copy the code

Initialization list

Sometimes, when you are implementing a constructor, you need to do some initialization before the constructor body is executed. For example, fields modified by final must be assigned before the constructor body is executed. Do this in the initializer list between the constructor’s signature and its function body:

Point.fromJson(Map<String, num> json)
    : x = json['x'],
      y = json['y'] {
  print('In Point.fromJson(): ($x, $y)');
}
Copy the code

The initializer list is also a convenient place to place assertions, which are only run during development:

NonNegativePoint(this.x, this.y)
    : assert(x >= 0),
      assert(y >= 0) {
  print('I just made a NonNegativePoint: ($x, $y)');
}
Copy the code

The extension operator “…”

Dart introduced the extension operator in 2.3 (…) And null-aware extension operators (… ?). , they provide a neat way to insert multiple elements into a collection.

For example, you can use the extension operator (…) Insert all elements from one List into another List:

var list = [1.2.3];
var list2 = [0. list];assert(list2.length == 4);
Copy the code

If the right side of the extension operator may be NULL, you can use the null-aware extension operator (… ?). To avoid generating exceptions:

var list;
var list2 = [0. ? list];assert(list2.length == 1);
Copy the code

See extension operator advice for more information on how to use extension operators.

If in a set

Dart also introduces both the if in collections and the for operation in collections, allowing you to use conditional judgments (if) and loops (for) when building collections.

The following example is an example of using if from a collection to create a List, which may contain three or four elements:

var nav = [
  'Home'.'Furniture'.'Plants'.if (promoActive) 'Outlet'
];
Copy the code

Lazy loading

In the following example, if the temperature variable is never used, then the expensive _readThermometer() function is never called:

 // This is the program's only call to _readThermometer().
 late String temperature = _readThermometer(); // Lazily initialized.
Copy the code

Formatting JSON String

String prettyJsonString(dynamic json) {
  JsonEncoder encoder = new JsonEncoder.withIndent(' ');
  String jsonString = encoder.convert(json);

  return jsonString;
}
Copy the code

Dynamic to string

static Object toEncodableFallback(dynamic object) {
  return object.toString();
}

static String stringifyMessage(dynamic message) {
  final finalMessage = message is Function ? message() : message;
  if (finalMessage is Map || finalMessage is 可迭代) {
    var encoder = JsonEncoder.withIndent(' ', toEncodableFallback);
    return encoder.convert(finalMessage);
  } else {
    returnfinalMessage.toString(); }}Copy the code

Incurable diseases

Interaction range of the GestureDetector

Because in development, there are highly customized UI components, like buttons, that are basically implemented by wrapping GestureDetector

The problem is that wrapped components are always hard to click (triggering events like onTap), especially “text buttons”

In practice, it can be solved by setting the background color

Transparent Colors. Transparent also works if you don’t need a colored background

GestureDetector(
  child: Container(
    width: 67,
    height: 40,
    alignment: Alignment.center,
    color: Colors.transparent, // <= Experience the effect with or without this line of code
    child: Text('cancel'),
  ),
  onTap: () => print('onTap'),Copy the code

Reference documentation

  • dart.cn/guides