Ilia Kurtov
Ilia Kurtov

Reputation: 1090

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?

Upvotes: 26

Views: 12601

Answers (5)

Usman ali
Usman ali

Reputation: 21

This is my extension and i have created this extension inside string_extension.dart

extension EmailValidator on String {
  bool isValidEmail() {
    return RegExp(
            r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
        .hasMatch(this);
  }
}

So in order to make this extension work you just need to import this extension_file like

import 'package:projectname/extensions/string_extension.dart';

and then

final String email = '[email protected]';
if(email.isValidEmail()){
}

Upvotes: 1

Giles Correia Morton
Giles Correia Morton

Reputation: 872

In case this helps someone in the future I had an issue with using extensions on final rather than the class name. I had:

extension StringExtensions on String {
  bool isFuture() {
    ...
  }
}

final time = 'some time of day';
if (time.isFuture()) {
...
}

This was throwing an error, to solve it I needed to do:

final String time = 'some time of day';

Upvotes: 1

Jayadeep
Jayadeep

Reputation: 329

You have to update the sdk version in pubspec.yaml to 2.6

enter image description here

Upvotes: 0

Vlad Loboda
Vlad Loboda

Reputation: 55

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

Upvotes: 2

Erick M. Sprengel
Erick M. Sprengel

Reputation: 2219

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

Upvotes: 38

Related Questions