Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart extension functions not found

I have a project on flutter and I want to use extension methods there very much. I've upgraded pubspec.yaml file to use sdk: ">=2.6.0 <3.0.0". I create a file list_extensions.dart with content

import 'dart:math';

extension ListExtension<T> on List<T> {

  T randomElement() => this.elementAt(Random().nextInt(this.length));

}

When I try to use this extension method in other files like this

String getRandomText(List<String> texts) => texts.randomElement();

I see a compiler error with text: The method 'randomElement' isn't defined for the class 'List'.

But when I try to use this extension method inside list_extensions.dart file - compiler is ok with that.

The thing is that my other project can found all extension methods that I declare.

flutter doctor says:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v1.10.17-pre.74, on Mac OS X 10.15 19A603, locale en-RU)
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    ✗ Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit https://flutter.dev/setup/#android-setup for detailed instructions.
[✓] Xcode - develop for iOS and macOS (Xcode 10.2)
[✓] Android Studio (version 3.5)
[✓] Android Studio (version 3.4)
[✓] VS Code (version 1.39.2)
[✓] Connected device (1 available)

flutter --version says:

Flutter 1.10.17-pre.74 • channel master • https://github.com/flutter/flutter.git
Framework • revision bcc93bca23 (6 days ago) • 2019-11-13 11:31:20 -0800
Engine • revision 31cd2dfca2
Tools • Dart 2.7.0

What can I do to force the compiler to see my extension methods?

like image 390
Ilia Kurtov Avatar asked Nov 19 '19 21:11

Ilia Kurtov


People also ask

How do you extend a dart function?

Dart has a Function type. This can be extended on and you can pass type parameters if you want. Here is an example from the changelog: extension CurryFunction<R, S, T> on R Function(S, T) { ... }

What is extension method in flutter?

Extension methods are new as of Dart 2.7. They allow you to add functionality to existing libraries and classes. For example, you can add extra functionality to the Dart core String library, that are only available in your app.

What is extension method function?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.


2 Answers

Extension methods were introduced in Dart 2.7, so you can change your sdk version: ">=2.7.0 <3.0.0"

like image 44
Vlad Loboda Avatar answered Sep 19 '22 08:09

Vlad Loboda


It was not your case, but for me, it worked after updating the Android Studio, Flutter plugin and Dart plugin. Maybe it'll help someone else.

Version that extension methods are working:

  • Android Studio 3.6.2
  • Flutter Plugin 192.7761
  • Dart Plugin 45.1.1

And another important point is that extension must be named. If it's not named it can't be used in other files:

Private extension:

extension on Banana {
  // ...
}

Public extension:

extension BananaExtension on Banana {
  // ...
}

I found it in a comment on Dart extension announcement on YouTube

like image 161
Erick M. Sprengel Avatar answered Sep 18 '22 08:09

Erick M. Sprengel