error in importing Http/http.dart package - flutter

I updated the dependencies with proper indentation, then flutter pub get and then import it in the code. Tried to restart my ide as well. The error pops up when i try to build the application.
Error:
Unable to find modules for some sources, this is usually the result of
either a bad import, a missing dependency in a package (or possibly a
dev_dependency needs to move to a real dependency), or a build failure
(if importing a generated file).
Please check the following imports:
import 'package:http/http.dart' as http; from myapp|lib/myapp.dart
at 83:1
Code:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
void main(){
runApp(MaterialApp(
home: HomePage(),
));
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Future getData() async{
http.Response response = await http.get("http://dummy.restapiexample.com/api/v1/employees");
debugPrint(response.body);
}
#override
void initState() {
super.initState();
getData();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Employees"),
backgroundColor: Colors.green,
),
);
}
}
pubspec.yaml
name: myapp
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
http: ^0.12.0+4
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
fonts:
- family: IndieFlower
fonts:
- asset: fonts/IndieFlower-Regular.ttf
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages

Have you tried restarting your IDE?
https://flutter.dev/docs/development/packages-and-plugins/using-packages
Stop and restart the app, if necessary If the package brings
platform-specific code (Java/Kotlin 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.

I see your Flutter version is 1.13.6.
I also experienced some strange problems with this version, but when I switched to dev channel all the problems are gone.
Try this too:
flutter channel dev
flutter upgrade

do not use vscode run&debug feature , use this command to run flutter run -d web-server --web-hostname= --web-port= --profile -v ,replace "ipaddress" and "freeport" with your system input

Related

package is forbidden but only when it's imported

Here's an error I'm getting when trying to save the pubspec.yaml (which automatically runs flutter pub get) (I changed the names of the packages):
Running "flutter pub get" in ravencoin_front...
Because every version of package_b from path depends on
package_c any which is forbidden, package_b from path is
forbidden. So, because package_a depends on package_b from
path, version solving failed. pub get failed (server unavailable) --
attempting retry 1 in 1 seconds...
So the question is, why is package_c forbidden? Here's what package_c looks like:
pubspec.yaml:
name: package_c
version: 1.0.0
description: Serverpod client for communication to the consent server and database.
homepage: https://github.com/.../package_c
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
serverpod_client: ^0.9.8
/lib/package_c.dart
library package_c;
export 'package:package_c/src/protocol/protocol.dart';
export 'package:serverpod_client/serverpod_client.dart';
Finally here's protocol.dart:
/* AUTOMATICALLY GENERATED CODE DO NOT MODIFY */
/* To generate run: "serverpod generate" */
// ignore_for_file: public_member_api_docs
// ignore_for_file: unnecessary_import
library protocol;
// ignore: unused_import
import 'dart:typed_data';
import 'package:serverpod_client/serverpod_client.dart';
import 'consent_class.dart';
import 'consent_document_class.dart';
export 'consent_class.dart';
export 'consent_document_class.dart';
export 'client.dart';
class Protocol extends SerializationManager {
static final Protocol instance = Protocol();
final Map<String, constructor> _constructors = {};
#override
Map<String, constructor> get constructors => _constructors;
Protocol() {
constructors['Consent'] = (Map<String, dynamic> serialization) =>
Consent.fromSerialization(serialization);
constructors['ConsentDocument'] = (Map<String, dynamic> serialization) =>
ConsentDocument.fromSerialization(serialization);
}
}
There are no errors, not sure why this package is seen as "forbidden."
Now package_b also requires other pacakges and overrides their paths, and they work just fine. so at first I thought it doesn't like the "nested paths" part of this situation, but that's not it, otherwise it would complain about the others too.
I also wondered, maybe the issue is that it has two "library" listed in the files. but I don't think that's it because if I remove the library package_c; it has the same problem.
Maybe it's as simple as this package_c not being correctly setup as a package, but I think it is, I looked up the requirements and it seems to fit from what I found.
Just for good measure let me include some of package_b pubspect.yaml:
name: package_b
description: Ravencoin wallet backend library
version: 1.0.0
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
...
package_d: ^4.0.1
...
package_c: ^1.0.0
dependency_overrides:
# causes error!
package_c:
path: ../../package_c
# works fine!
package_d:
path: ../../package_d
...
Now, Here's the really weird part. remember, package_a imports pacakge_b which imports package_c. Well, package_b works just fine, dart pub get works great. but when I run flutter pub get in package_a, the error at the top occurs. the weird thing is only package_a complains about package_c, package_b is fine with package_c... what?
I guess I should include part of the pubspec.yaml for package_a too, this is the one that causes the error:
name: package_a
description: Ravencoin wallet frontend
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+0
environment:
sdk: '>=2.14.0 <3.0.0'
dependencies:
...
flutter:
sdk: flutter
package_b: ^1.0.0
...
dependency_overrides:
package_b:
path: ../package_b
...
...

The method 'watchPerformance' isn't defined for the type 'IntegrationTestWidgetsFlutterBinding'

I'm currently doing the integrated_test codelab on https://codelabs.developers.google.com/codelabs/flutter-app-testing/#6.
I'm on step 6 where I have to test the performance of the test_app:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:testing_app_codelab/main.dart';
import 'package:testing_app_codelab/testing_app.dart';
void main() {
group('Testing App Performance Tests', () {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized()
as IntegrationTestWidgetsFlutterBinding;
binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
testWidgets('Scrolling test', (tester) async {
await tester.pumpWidget(TestingApp());
final listFinder = find.byType(ListView);
await binding.watchPerformance(() async { // error is here
await tester.fling(listFinder, Offset(0, -500), 10000);
await tester.pumpAndSettle();
await tester.fling(listFinder, Offset(0, 500), 10000);
await tester.pumpAndSettle();
}, reportKey: 'scrolling_summary');
});
});
}
I've gone through the IntegrationTestWidgetsFlutterBinding documentation and watchPerformance() is a method of it(please check: https://api.flutter.dev/flutter/package-integration_test_integration_test/IntegrationTestWidgetsFlutterBinding/watchPerformance.html).
The error I get on VS code is:
The method 'watchPerformance' isn't defined for the type 'IntegrationTestWidgetsFlutterBinding'.
Try correcting the name to the name of an existing method, or defining a method named 'watchPerformance'.
When I run the Integration test on my Android phone I get this error:
Try correcting the name to the name of an existing method, or defining a method named 'watchPerformance'.
await binding.watchPerformance(() async {
I got the idea to solve this problem from this post Flutter / Dart package integration_test with null safety
The problem was that integration_test dependency doesn't support null-safety.
when you check which dependencies have null safety support using this command:
dart pub outdated --mode=null-safety
The output is:
Showing dependencies that are currently not opted in to null-safety.
[✗] indicates versions without null safety support.
[✓] indicates versions opting in to null safety.
Package Name Current Upgradable Resolvable Latest
direct dependencies:
integration_test ✗0.8.1 ✗0.8.1 ✗0.8.1 ✗1.0.2+3
dev_dependencies: all support null safety.
You are already using the newest resolvable versions listed in the 'Resolvable' column.
Newer versions, listed in 'Latest', may not be mutually compatible.
This means that it's legacy and doesn't have support.
To solve this I changed the pubspec.yaml from this:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
provider: ^6.0.1
test: ^1.19.5
integration_test: ^0.8.1
dev_dependencies:
flutter_test:
sdk: flutter
Changed to this instead:
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
provider: ^6.0.1
test: ^1.19.5
dev_dependencies:
integration_test:
sdk: flutter
flutter_test:
sdk: flutter
After this, I ran flutter pub get on the terminal and the error disappeared.
Now when I hover over the watchPerformance() method I get this info:
Future<void> watchPerformance(Future<void> Function() action, {String reportKey = 'performance'})
package:integration_test/integration_test.dart
Watches the [FrameTiming] during action and report it to the binding with key reportKey.
This can be used to implement performance tests previously using [traceAction] and [TimelineSummary] from [flutter_driver]
This is a general programming error that will occur. I will put an answer in because the error description for these is often rubbish at telling you what is going on here.
The error is telling you the function called watchPerformance on the object IntegrationTestWidgetsFlutterBinding (a var here called binding), does not exist.
Does the function use a captial W instead of lower case? Take another look at the docs and check everything is spelled correctly etc.

Incomplete import in the generated_plugin_registrant.dart

I have a plugin created using the federated approach. The problem is that the generated_plugin_registrant.dart contains an incomplete import which is pointing to the app-facing package but leaves out the dart file name and this causes an error.
import 'package:my_plugin/'; // PROBLEMATIC LINE
import 'package:my_plugin_web/my_plugin_web.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
// ignore: public_member_api_docs
void registerPlugins(Registrar registrar) {
.registerWith(registrar); // PROBLEMATIC LINE
MyPluginWeb.registerWith(registrar);
registrar.registerMessageHandler();
}
This code is in the default example app for a plugin with an unchanged default pubspec.yaml.
Here's the pubspec.yaml for the app-facing "my_plugin" package:
name: my_plugin
description: A new flutter plugin project.
version: 0.0.1
author:
homepage:
environment:
sdk: '>=2.12.0 <3.0.0'
flutter: '>=1.20.0'
dependencies:
flutter:
sdk: flutter
my_plugin_platform_interface:
path: ../my_plugin_platform_interface
my_plugin_web:
path: ../my_plugin_web
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
plugin:
platforms:
android:
package: com.example.my_plugin
pluginClass: MyPlugin
ios:
pluginClass: MyPlugin
web:
defaultPackage:
path: my_plugin_web
I've tried all the usual commands like flutter clean and recreating the project but to no avail.
Under your web definition (which Google wants in a separate project you have to specify it as
web:
pluginClass: The class name of your web class here
fileName: The file name of the file that holds your web class here
I had it has just "file" and yours is "path" which will cause this.
The issue is that plugin support for the web was not generated for a specific package or packages.
Try creating an analysis_options.dart and add these lines
analyzer:
exclude: [lib/generated_plugin_registrant.dart]
then configure your vscode/.settings.json with these lines
"dart.analysisExcludedFolders":["./yourApp/lib/generated_plugin_registrant.dart" ]
Just find the answer, basically you need to use default_package to specify your are using the integrated plugin. More here

Trying to launch a URL in browser from my flutter app. When I click the button I created to launch the URL nothing happens. Don't know why

I am trying to create an app that allows the user to click a button and have a website open in their browser. I wrote this code with a lot of help from the Flutter docs. Everything compiles and runs fine but when I click the button in the middle of the screen that is supposed to launch the URL nothing happens. The button just does what I think is a splash animation but the URL is not launched in the browser as intended. This doesn't have anything to do with the fact that I am using an emulator to test this does it? Or is a problem with the url_launcher package? I will attach my code and the .yaml file. Would appreciate any response from anyone. Thank you very much!
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Your Covid-19 Briefing"),
),
body: Center(
child: RaisedButton(
onPressed: _launchUrl,
child: Text("Covid-19 Cases"),
),
),
),
);
}
_launchUrl() async {
const _urlToLaunch = "https://www.worldometers.info/coronavirus/";
if (await canLaunch(_urlToLaunch)) {
await launch(_urlToLaunch);
} else {
throw "Could not open $_urlToLaunch";
}
}
}
name: coronavirus_news_updates
description: A new Flutter application.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
url_launcher: ^5.4.11
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
Does it throw any error?. I tried running the above code with both emulator and my smartphone and it works fine.

Flutter: Unimplemented handling of missing static target

I am running my first Flutter project
I created a sample project and I was able to run the app in
simulator
I have edited the file main.dart
main.dart
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: Text('Hello World')));
Now I am getting the error :
pubspec.yaml
name: flutter_app
description: A new Flutter application.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
The same error occurred for me at first ... but it worked when I did a 'hot restart'
Just compile the project again and it will work. The error occurs because static variables are hard compiled on the executable and are not dynamic. If you hot reload the changes, it will not rebuild the executable (apk), so that's why you need to rebuild the app.
Your code works for me...
Have you tried a hot restart? The shortcut is CTRL+SHIFT+\ or just click the button with the green circle-arrow in the console-window
I'm not sure what that error is, since I can't replicate it. However, this is more like what your app should look like. Give this a shot and see if it works.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Text('Hello World'),
);
}
}
Just delete the build and recompile. It worked for me. Use the following code:
flutter clean
flutter run
Usually, a Full App Restart is required after a heavy logic change, as transforming a StatelessWidget into StatefulWidget. Check André Glatzl answer
In Android Studio use the Flutter Hot Restart
A hot restart will work. But the source of the problem might be as follows.
You've created new project. You've deleted boilerplate code and try to modify without a stateful or stateless widget. Flutter tries to rebuild widgets under build() method. But when you hot reload, flutter might not resolve what you've updated.
Another example of such error is, when we create a new static variable(static String token = "abc";) in one Class (like constants.dart) and try to reference them in another screen (constants.token), flutter failed to resolve such static things when we hotreload. but it work fine when we hotrestart.
One of the reasons this happens is you are running the app for the first time and then you make some changes and reload again. Without reloading, stop the execution, and again run the app from scratch, this error won't come.
I also faced the same situation, I fixed it by running the app again, This issue is caused when you applied a lot of change to your code and pressed the hot reload button.
Restarting the app worked in my case.
Run this command again
'flutter run'
should work
You changed the myApp and created your own MaterialApp so all you have to do is after changing the name into MaterialApp instead of hot restart click the play button. It will work.
Thanks
Just press the red stop button and click the play button again. It will work :)
As other answers mention, you can 'hot reload' or rebuild your app to solve this issue.
But the reason why I got this error, was that I have edited the pubspec.yaml file, when editing the pubspec.yaml file, you should stop and rebuild the entire app.
Navigate to your lib folder where your main.dart file is and run the commands below:
PS C:\Users\mobile_app\lib> flutter clean
PS C:\Users\mobile_app\lib> flutter pub get
PS C:\Users\mobile_app\lib> flutter run
although if you are having null safety issues I would prefer you debug your application first. In my encounters this error is of a result of trying to run a class component outside a the Static Widget with context hence the class is loaded before build time making the application fail to start.
I occurred when I was implementing TextEditingController and there after I created a function which has parameter inside, so when I wrote the addListener for the controller, then below error got occurred,
Unimplemented handling of missing static target flutter
but after removing the argument from the addListener function still the issue persists and which broked my application, even I did the hot reload in my Android Studio.
Solution:
Mostly similar to above said, but little different, I terminated my app, and rerun the app, and which fixed the issue.
Hope this would help manyone.
If you have added Hot Reloading extension then exit and try restarting the HOt Reloading.
It should work.
Check your import.
It should be
import 'package:your_app...' not import 'file://C/path/your_app/