Introduction

Generally speaking, if you want to extend a class, you need to inherit this class. This is what you need to do in most java or other object-oriented languages.

But sometimes extended classes are not particularly useful. First of all, in some languages, some classes are forbidden to be extended. Even if it can be extended, the extended class is a new class, not the original parent class, so some type conversion problems may occur during use.

So how is this problem solved in dart?

The use of extension in dart

Dart introduced extension after 2.7 to extend the methods of the class.

How to expand? Let's give an example.

We can convert a string to int by calling the parse method of int, as shown below:

int.parse('18')

But the conversion through the int class is usually not intuitive. We hope to provide a toInt method in the String class, which can be called directly to convert the string to an int.

'18'.toInt()

Unfortunately, String does not provide a toInt method, so we can extend String through extension:

extension StringToNumber on String {
  int toInt() {
    return int.parse(this);
  }
  // ···
}

If the name of this file is called string_to_number.dart, then we can use it like this:

import 'string_to_number.dart';
// ···
print('18'.parseInt()); 

The most convenient way to extend the method in dart is that you only need to introduce the corresponding lib, and you don't even know that you are using the extension of the lib when you use it.

Of course, not all classes can be extended with extension. For example, the dynamic type cannot be extended.

But using the var type, as long as the type can be inferred, then extension can be used.

API conflict

Now that the lib can be extended, there may be API conflicts. So how to resolve API conflicts?

For example, we need to use two lib extension files, extension1.dart and extension2.dart. But both extension files define the parseInt method to extend String.

If they are cited at the same time, problems will arise.

At this time, you can use show or hide to limit the specific method in which extension file is used.

import 'extention1.dart';

import 'extention2.dart' hide StringToNumber2;

print('18'.parseInt());

There is also a situation where extension is called explicitly, as shown below:

import 'extention1.dart';

import 'extention2.dart';

print(StringToNumber('18').parseInt());
print(StringToNumber2('18').parseInt());

Distinguish by the name of the extension.

If the two extensions have the same name, they can be distinguished by prefix:

import 'extention1.dart';

import 'extention2.dart' as ext2;

print(StringToNumber('18').parseInt());
print(ext2.StringToNumber('18').parseInt());

Realization of extention

The implementation of the extension is very simple, the implementation syntax is as follows:

extension <extension name> on <type> {
  (<member definition>)*
}

The following is an example of extending String:

extension NumberParsing on String {
  int parseInt() {
    return int.parse(this);
  }

  double parseDouble() {
    return double.parse(this);
  }
}

Extension can also extend generic parameters:

extension MyFancyList<T> on List<T> {
  int get doubleLength => length * 2;
  List<T> operator -() => reversed.toList();
  List<List<T>> split(int at) => [sublist(0, at), sublist(at)];
}

The above implementation is an extension of List<T>, adding getter, operator and split methods.

Summarize

The above is the new feature of 2.7, the extension of the class.

This article has been included in http://www.flydean.com/26-dart-extension-method/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!


flydean
890 声望437 粉丝

欢迎访问我的个人网站:www.flydean.com