Flutter extension-methods not working, it says "undefined class" and "requires the extension-methods language feature"
Asked Answered
A

6

13

I'm slowly building my personal website over at dlblack.dev, and I'm trying to spice it up a little bit. For example, from a computer (rather than a tablet or phone since they don't have mouse pointers), if you hover over any of the clickable items, it doesn't change your mouse pointer to indicate it's clickable, and the clickable object doesn't change at all. I've decided to follow this FilledStacks tutorial, but it doesn't mention anything about fixing this problem.

Essentially what's happening is when I get ~2.5 mins through the tutorial video (where he writes the skeleton extension class) and try to duplicate it, VS Code redlines almost the entire class declaration aside from the name. What I'm writing is the exact same thing as what he has on screen at 2:26, and here's my code:

import 'package:flutter/material.dart';
import 'dart:html' as html;

extension HoverExtension on Widget{
  
}

"extension", "on", and "Widget" are all redlined when I do this. When I hover over "extension", it says the following:

Undefined class 'extension'.
Try changing the name to the name of an existing class, or creating a class with the name 'extension'. dartundefined_class
This requires the 'extension-methods' language feature to be enabled.
Try updating your pubspec.yaml to set the minimum SDK constraint to 2.6 or higher, and running 'pub get'. dart(experiment_not_enabled)

The first thing I did was change my minimum SDK constraint to 2.6.0 in my pubspec.yaml file. I then changed it to 2.7.0 because a lot of people online say extensions were released in Dart 2.7. I've done a lot of Googling on the subject but no one seems to have the same problem as me: I have no analysis_options.yaml file. I created one, and put only this in its contents:

include:

analyzer:
  enable-experiment:
    - extension-methods

linter:

In theory, I believe that should fix my problem once I run flutter pub get from the command line in my root folder; it doesn't. I have no idea what's wrong. Any suggestions?

Ardenia answered 13/5, 2020 at 16:27 Comment(3)
What exactly is in your pubspec.yaml file?Interbedded
@Interbedded name: DLBlackDev description: My personal site built entirely in Flutter/Dart. version: 1.0.0+1 environment: sdk: ">=2.6.0 <3.0.0" dependencies: flutter: sdk: flutter google_fonts: ^0.3.7 cupertino_icons: ^0.1.3 responsive_builder: ^0.1.2 get_it: url_launcher: ^5.4.7 dev_dependencies: flutter_test: sdk: flutter flutter: uses-material-design: true assets: - assets/logo.png - assets/Dustin Black - Resume - 5-12-20.pdf - assets/me.jpg ...everything else is commented outArdenia
pastebin.com/3fYns6JD <-- there's the full file, the comment messed up the formattingArdenia
A
21

For the changes in pubspec.yaml and analysis_options.yaml to take place, you have to restart the Dart Analysis Server. In VSCode, that's as simple as Ctrl+Shift+P -> Reload Window.

Ardenia answered 14/5, 2020 at 0:24 Comment(0)
F
24

In Android Studio solving this problem requires three steps:

  1. Update SDK version in pubspec.yaml (must be 2.6.0 or higher)

     environment:
       sdk: ">=2.7.0 <3.0.0"
    
  2. Tools -> Flutter -> Flutter Clean

  3. Close project and reopen it (eg. File -> Close project)

Foyer answered 28/7, 2020 at 16:53 Comment(2)
Thank you very much. That's solved my problems in VSCode.Davie
I did everything but closing the project :D Thanks, it works nowPee
A
21

For the changes in pubspec.yaml and analysis_options.yaml to take place, you have to restart the Dart Analysis Server. In VSCode, that's as simple as Ctrl+Shift+P -> Reload Window.

Ardenia answered 14/5, 2020 at 0:24 Comment(0)
W
3

I had to do flutter clean, then close and re-open VSCode.

Winsome answered 21/7, 2020 at 23:23 Comment(0)
I
0

I had a similar problem with the "extensions" feature in Dart (on Windows) ... I was getting "undefined class", and the message saying that I needed to use the "experimental" Dart settings. It seems that I had two Dart installations, one in "c:\Program Files" and one in "c:\tools\dart-sdk". I removed the version under "Program Files". I also ran "choco uninstall dart-sdk" and "choco install dart-sdk", and made sure that the version was correct (using "dart --version") after the install. The command "where dart" should display "c:\tools\dart-sdk-bin-dart.exe" after the choco install. When I went back into my Flutter project (using Android Studio) it said that I didn't have Dart support for my project, and I just had to enter the Dart SDK location.

Involved answered 1/7, 2020 at 16:25 Comment(0)
H
0

if tombroz's solution won't work then

1.Tools -> Flutter -> Flutter Clean
2.Tools -> Flutter ->Pub get
3.File -> Invalidate cache/Restart -> Invalidate cache and Restart
Hallsy answered 2/3, 2021 at 6:53 Comment(0)
P
-1

I just had to run flutter clean et voila.

Pulsar answered 5/3, 2021 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.