Dart 2.7 adds support for extended methods and a new package for handling strings with special characters

① Extension: can add new functions to any type

• Any type 🈯 Everything is an object, including built-in types, as well as functions

② The extension method is static

• Extension methods are statically resolved and dispatched, which means you cannot call them Dynamic on values of type. Here, the call throws an exception ** at run time

[Code]
dynamic field = '1'; field .parseInt(); → Runtime exception: NoSuchMethodError'1';
field .parseInt();
Copy the code

③ Extensions can have type variables

• Imagine that we want to define an extension, List, to make elements reach an even index. We want this extension to work with any type of list and return a new list of the same type as the input list. We can do this by making the extension generic and applying the type parameters of the extension to the extension and extension methods

[Code]
extension FancyList<T> on List<T> {
  List<T> get evenElements {
    return <T>[for(int i = 0; i < this.length; i += 2) this[i]]; }}Copy the code

③ Extension methods actually expand members

• We call this the feature extension approach, because it’s a familiar term if you use the corresponding language feature in another programming language. But in Dart, this functionality is more general: it also supports the use of new getter, setter, and Operator extension classes. In the FancyList example above, evenElements is an getter. This is an example of adding operators for moving strings

[Code]
extension ShiftString on String {
  String operator <<(int shift) {
    return this.substring(shift, this.length) + this.substring(0, shift); }}Copy the code

⑤ Safe substring processing ⭕

📃 did not understand the document, I have time to overwrite it.

⑥ Air safety preview ⭕

📃 did not understand the document, I have time to overwrite it.

Personal Daily Notebook 💻 Rebook’s macPro