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

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?

In Android Studio solving this problem requires three steps:
Update SDK version in pubspec.yaml (must be 2.6.0 or higher)
environment:
sdk: ">=2.7.0 <3.0.0"
Tools -> Flutter -> Flutter Clean
Close project and reopen it (eg. File -> Close project)

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.

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

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.

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

I just had to run flutter clean et voila.

Related

After flutter new project creation I can't locate the main dart file to start programming

I have installed Android Studio and flutter successfully. When I run flutter doctor in command prompt it gives me [all green ticks in these brackets].
The question is that after the creation of a new flutter project on the right-hand side of the screen under the project I am unable to see/locate any dart main file to write code. For further clarification, you can see the attached image below.
Unable to locate the dart file to write code in the project hierarchy:
I have 0 ideas what is still missing in the configuration of Flutter / Android Studio
Well, I figured out why pubspec.yaml and main.dart was not in my project due to the older version of flutter. I downloaded the latest version flutter_windows_2.2.2-stable, created a new flutter project, and included the flutter SDK path of flutter_windows_2.2.2-stable which resolved all my problems. Kindly, make sure whoever wants to start Mobile Application development in Android Studio + Flutter + Dart always download the latest version of everything.
Recent Experiment:
After downloading the latest version of Flutter still, I was unable to get the pubspec.yaml and main.dart files in my Android Studio Project, so the solution is below:
where you have extracted your flutter_windows_2.2.2-stable remember the directory path like E:\src\flutter_windows_2.2.2-stable which is in my scenario.
******************* CMD WORK *******************
Open cmd(Command Prompt) by default you will be in the directory like C:\Users\UserName\
Now go to the path E:\src and to navigate write E: hit enter, now you will be in the E: directory. Write cd E:\src hit enter.
type flutter create project_name ( make sure whenever you withe the name of the project it should be like this my_first_app in this format you name the projects with proper naming convention in flutter [word_word_word_word] ) then hit enter, wait for a while it will create a new flutter project for you including pubspec.yaml and main.dart files + necessary files to be imported.
Reference for naming convention in flutter (https://medium.com/flutter-community/file-and-folder-structure-in-flutter-967b8be3155e#:~:text=Flutter%20doesn't%20have%20a,letters%20and%20underscores%20between%20words.&text=When%20naming%20folders%2C%20you%20want,them%20very%20direct%20and%20clear.)
Right now after the creation of a project(Flutter) in Android Studio you are unable to see/locate the pubspec.yaml and main.dart files use cmd for the project creation which worked in my scenario.

Error setting-up flutter_localizations package "Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'."

This error appears when I try to add language support to my flutter application:
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist.
I’m using flutter_localizations, the project is built with dart 2.7.0
It seems that flutter_localizations needs latest sdk which is dart 2.13.0
How can I fix this?
I had this same issue with import 'package:flutter_gen/gen_l10n/app_localizations.dart' in Visual Studio Code after updating to Flutter 2.5.
After closing the VS Code window and reopening it, the error was gone.
If that doesn't work, try closing and opening VS Code again.
Or restart the Dart Analysis Server by going to View > Command Palette and then typing Dart: Restart Analysis Server.
Credit goes to https://stackoverflow.com/a/68150402/4797855 and https://stackoverflow.com/a/68731436/4797855.
Open Command Palette and select Dart: Restart Analysis Server.
Just type in the project terminal flutter gen-l10n it will solve your problem.
Also, in the pubspec.yaml file, enable the generate flag. This is added to the section of the pubspec that is specific to Flutter, and usually comes later in the pubspec file.
# The following section is specific to Flutter.
flutter:
generate: true # Add this line
just in Your terminal run this
flutter pup add flutter_gen/gen_l10n
It fixed my app
I had this issue before, and I tried to solve it by adding charts_flutter to my pubspec.yaml but it didn't solve my.
Lucky me, charting library recently released a new version which is compatible with flutter 2.5. I didn't need to add anything to my pubspec(I removed chart_flutter from my ). just run the app even if the compiler shows an error saying Target of URI doesn't exist for the import line. The system is going to generate that file on runtime.
Be sure that there's an l10n.yaml file in your project root as follows.
Example
Demonstration variables
foldername = l10n
defaultIntlFile = intl_en.arb
importFileName = app_localizations.dart
Import
import 'package:flutter_gen/gen_l10n/importFileName';
l10n.yaml
arb-dir: lib/foldername
template-arb-file: intl_en.arb
output-localization-file: app_localizations.dart
Structure
lib/foldername/defaultIntlFile
The solution in my case:
"If you are using a package flutter_gen you need to remove it from pubscpec.yaml to resolve conflict." credit Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'

ERROR with flutter: CocoaPods not installed even if it is installed

I am trying to run my code on AndroidStudio by using Flutter but when I run it shows an error:
Warning: CocoaPods not installed. Skipping pod install. CocoaPods is
used to retrieve the iOS and macOS platform side's plugin code that
responds to your plugin usage on the Dart side. Without CocoaPods,
plugins will not work on iOS or macOS. For more info, see
https://flutter.dev/platform-plugins To install see
https://guides.cocoapods.org/using/getting-started.html#installation
for instructions.
CocoaPods not installed or not in valid state. Error launching
application on iPhone 11.
flutter doctor shows that all is fine, I looked around a lot and tried lots of things like uninstall it and install it again, and tried adding:
dev_dependencies:
build_runner: ^1.10.0
dependency_overrides:
analyzer: 0.39.14
and tried
flutter channel stable
flutter upgrade
and tried
flutter channel master
flutter upgrade
But nothing worked, note that when I write "which pod" it shows the path, that means it exists right? then what is the problem? please help :(
have you tried to run pod install from the ios folder?
The only thing that worked with me is using android emulator, ios simulator still show the same error
I had the same issue and I found a solution, but with still an answered question. See my question (and one partial solution) here
The partial solution is: in Android Studio, click on the tab "Terminal" at the bottom of the IDE and in this pane, type flutter run. It does the same as the button Run, and Android Studio stopped complaining about CocaPods.
To stop the app, type q in the same terminal.

How to add a package in the android studio - flutter?

I started fluttering a few days ago. I want to add "font awesome" to my project, but I can't. I searched but could not find the right answer. I finally got this guide, but it doesn't work either.
Adding a package dependency to an app
To add the package, css_colors, to an app:
Depend on it
Open the pubspec.yaml file located inside the app folder, and add css_colors: under dependencies.
Install it
From the terminal: Run flutter pub get.
OR
From Android Studio/IntelliJ: Click Packages get in the action ribbon at the top of pubspec.yaml.
Import it
Add a corresponding import statement in the Dart code.
4. Stop and restart the app, if necessary
If the package brings platform-specific code (Kotlin/Java for Android, Swift/Objective-C for iOS), that code must be built into your app. Hot reload and hot restart only update the Dart code, so a full restart of the app might be required to avoid errors like MissingPluginException when using the package.
In the dependencies: section of your pubspec.yaml, add the following line:
dependencies:
font_awesome_flutter:
Look here https://pub.dev/packages/font_awesome_flutter/install
It contain examples and instructions.
Steps also the same:
add the package name to dependencies section. It very important to keep the correct number of spaces in every line of pubspec.yaml, it has very sensitive format
in your case:
dependencies:
font_awesome_flutter: ^8.10.0
Click "Packeges get" in top right corner
Import the package into your dart code
in your case:
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
Now your can use it in Icon widget
Icon(
FontAwesomeIcons.mars,
size:80.0)

The pubspec.lock file has changed since the .dart_tool/package_config.json file was generated

I'm running a dart only test in intellij and receiving the following error:
The pubspec.lock file has changed since the .dart_tool/package_config.json file was generated, please run "pub get" again.
I have tried pub get a number of times. Any ideas? I'm on the dev branch of Flutter (anything using Dart 2.7 pretty much fails with the same or a similar error message).
Edit
I think that if you have a path reference dependency in the pubspec.yaml file it will fail.
Anything like the following fails with the above message if you try to run a dart test in IntelliJ.
dependencies:
test_dependency:
path: ../test_dependency
I'm not suggesting this is a fix for everyone, but it did resolve my particular situation.
Just ran into this error after upgrading flutter from 1.22.6 to 2.0.2 and trying to use build_runner: ^1.12.2 and json_serializable: ^4.1.0 to generate json serialization/deserialization classes & methods.
The problem is apparently related to the pub package when used with relative paths for packages as noted in the following github issues:
https://github.com/dart-lang/pub/issues/2806
https://github.com/dart-lang/pub/pull/2830
One of the commenters in issue 2806 mentioned running flutter create . inside the flutter project folder which rebuilds .dart_tool/package_config.json, adding any missing entries, solving the problem.
No one solved this problem so far?
I've found a staightforward solution and it works fine for me.
You need to close your IDE (VSCode in my case), make sure that is completely closed using process-monitor.
Then open a terminal window manualy in your project folder and run "flutter pub get"
After that just start your IDE normaly and ewerything will work fine.