Introduction to the

In general, to extend a class, you need to inherit the class, which is what you do in most Java or other object-oriented languages.

But sometimes extending classes is not particularly useful. First, 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 there may be some conversion problems in the process of using it.

So how does this work in DART?

Use of Extension in DART

Dart introduced Extension after 2.7 to extend the methods of a class.

How do you scale it? Let’s take an example.

We can convert a string to an int by calling int’s parse method, as follows:

int.parse('18')
Copy the code

However, it’s not always straightforward to use the int class to do the conversion. We want to provide a toInt method in the String class that we can call directly to convert a String to an int.

'18'.toInt()
Copy the code

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

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

Dart if the file is called string_to_number.dart, we can use it like this:

import 'string_to_number.dart'; / /, print (' 18 '. The parseInt ());Copy the code

The most convenient thing about method extensions in DART is that you just import the corresponding lib and don’t even know you’re using the lib extension.

Of course, not all classes can be extended using Extention. The Dynamic type, for example, cannot be extended.

But with the var type, extention extensions can be used as long as the type can be inferred.

API conflict

Now that lib can be extended, API conflicts are possible. So how do you resolve API conflicts?

Dart and extention2.dart are two lib extension files that need to be used. However, the parseInt method is defined in both extension files to extend String.

If you quote them at the same time, there’s a problem.

You can use show or hide to restrict which methods are used in the extension file.

import 'extention1.dart';

import 'extention2.dart' hide StringToNumber2;

print('18'.parseInt());
Copy the code

There is also a case where extension is displayed as follows:

import 'extention1.dart';

import 'extention2.dart';

print(StringToNumber('18').parseInt());
print(StringToNumber2('18').parseInt());
Copy the code

Distinguished by extention’s name.

If two extentions 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());
Copy the code

The realization of the extention of

Implementing the extension is simple, and the syntax is as follows:

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

Here is an example of an extension String:

extension NumberParsing on String { int parseInt() { return int.parse(this); } double parseDouble() { return double.parse(this); }}Copy the code

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)];
}
Copy the code

The above implementation extends the List to add getters, operators, and split methods.

conclusion

That’s what’s new in 2.7, class extensions.

This article is available at www.flydean.com/26-dart-ext…

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!