Not able to import the intl package for dart - dart

Hi I am trying to use the intl package in my flutter project. The packages get command runs successfully but when I import the package it shows error.
For the import in the my dart file I am using the following import
import 'package:intl/intl.dart';
I have also upgraded flutter from the terminal using the flutter upgrade command.

Here are some steps that I offten see as problems with dependencies in flutter.
The thing with pubspec.yaml is that you have to save it CRTL/CMD + S before pub get will work. Running pub get from IDE doesn't save the file automatically.
Try running flutter clean and then run flutter pub get.
Sometimes when you remove dependencies like plugins, .build folder doesnt get cleaned up properly so you have to do it manually.
You can try to repair pub cache by running flutter pub cache repair
Sometimes just restarting your IDE might solve the problem as well.

Faced the same issue, if you are importing
import "package:intl/intl_browser.dart";
then you will get that error, it seems like the dart SDK bundled with flutter does not have the required dependencies.
So use only
import 'package:intl/intl.dart';
and it should work just fine.

It needs to reload the flowing package using CTRL+S might not work sometimes. Need to restart IDE after saving pubspec.yaml file.

After the project cleaning you could have some error like this:
intl_browser.dart:13:8: Error: Not found: 'dart:html'
You can clean your cache, make all you want to refresh your flutter project:
- flutter clean
- rm -rf pubspec.lock .packages .flutter-plugins
- flutter pub pub cache repair
- flutter packages get
But you can't solve until you comment this line on your code :
findSystemLocale()
This line is in conflict with dart:html as you can see here below according with the current intl source:
library intl_browser;
import "dart:async";
import "dart:html";
import "intl.dart";
// TODO(alanknight): The need to do this by forcing the user to specially
// import a particular library is a horrible hack, only done because there
// seems to be no graceful way to do this at all. Either mirror access on
// dart2js or the ability to do spawnUri in the browser would be promising
// as ways to get rid of this requirement.
/// Find the system locale, accessed as window.navigator.language, and
/// set it as the default for internationalization operations in the
/// [Intl.systemLocale] variable.
Future<String> findSystemLocale() {
Intl.systemLocale = Intl.canonicalizedLocale(window.navigator.language);
return new Future.value(Intl.systemLocale);
}

It started working for me after I inserted one tab before 'intl'. Final set of code looks here...
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.3
intl: ^0.16.1

If you need to be able to use findSystemLocale() on both web and other platforms, you can use conditional import:
import 'package:intl/intl_standalone.dart' if (dart.library.html) 'package:intl/intl_browser.dart';
After that you can just write
Intl.defaultLocale = await findSystemLocale();
You will be able to build on any platform.

I had the same issue on Visual Studio Code with the latest stable version of flutter. none of the previous answers helped in my case.
Running pub cache repair, which was suggested by some of the answers, used up all my data since it started downloading all flutter packages regardless of whether I used them or not and didn't solve anything.
The solution that worked for me:
Instead of running flutter pub get package_name in the terminal, I had to add the package under the dependencies in "pubspec.yaml" file manually like so:
dependencies:
package_name: ^1.1.0
and save it.
After saving it the IDE would get the packages for me and everything worked.

I fixed it with a combination of things from this question and here.
Firstly, import intl_standalone in your main.dart, not intl_browser.
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:intl/intl_standalone.dart';
Then call the necessary setup function, like this:
void main() async {
Intl.defaultLocale = await findSystemLocale();
await initializeDateFormatting();
runApp(MyApp());
}
Make sure to add intl to your deps, like this:
dependencies:
flutter:
sdk: flutter
intl:
Then run the following commands in order from your project directory:
flutter clean
rm -rf pubspec.lock .packages .flutter-plugins
flutter pub cache repair
flutter pub get
With this I could get it to work when building to Android and iOS.

Bit late but this happened to me, check the name of your files isn't the same name as the package you want to import.

Worked for me by manually adding intl: ^0.18.0 in pubspec.yaml and saving it (Cmd+S).
dependencies:
flutter:
sdk: flutter
intl: ^0.18.0
Wherever required import as import 'package:intl/intl.dart';

Related

Dart import a package you have created

I have some code that i want to use between two projects. I would like to break this out into a package and import it into other projects/packages that need it. When i try to import a locally created package, I get a "The target URI doesnt exist" error. What do i need to do? Is there a path environment variable that dart checks to find local packages? Do i need to publish my package to pub.dev and use "dart pub add"? (I really would not prefer this)
Thanks in advance
The dependencies section of pubspec.yaml supports, along with the pub.dev and Git options, a path option.
Path dependencies are documented on the Dart website.
For example:
dependencies:
hosted_package: ^1.0.0
local_package:
path: ../my_local_package

Flutter dart dependency issue

I need to have a webview inside my context body, but not to cover the entire screen space as I have to display a listview below it.
I checked flutter_webview_plugin but it did not work
Getting the error :
That library is in a package that is not known. Maybe you forgot to mention it in your pubspec.yaml file?
dependencies:
flutter:
sdk: flutter
url_launcher: ^0.4.1
async_loader: "^0.1.1"
json_annotation: ^0.2.2
flutter_webview_plugin: "^0.1.5"
cupertino_icons: ^0.1.0
#url_launcher: ^3.0.0
dev_dependencies:
flutter_test:
sdk: flutter
json_serializable: ^0.4.0`
I have written below line in my dart file
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
If you add a new package to your dependencies, then do a hot reload, you get the following misleading message:
Performing hot reload...
Your application could not be compiled, because its dependencies could not be
established.
The following Dart file:
/usr/local/google/home/ianh/dev/release/startup_namer/lib/main.dart
...refers, in an import, to the following library:
package:package:flutter_webview_plugin/flutter_webview_plugin.dart
That library is in a package that is not known. Maybe you forgot to mention it in
your pubspec.yaml file?
Try again after fixing the above error(s).
Kill your application and start it again. This might solve your issue.
For more references please visit this git hub discussion
Go to the build directory, which will be located at the top level of your app along with lib, ios, android, etc.
If a flutter_webview_plugin folder is not there, then you need to run flutter pub get to download the package.
If the folder is there, then it has already been downloaded. If that is the case and your IDE is still telling you that it does not recognize the package, then try restarting your IDE.

How do I import a package in a simple Dart script (no pubspec.yaml)?

Let's say I have a simple script such as this:
// File main.dart
import 'package:uuid/uuid.dart';
main() {
print(new Uuid().v1());
}
And I run it from command line via dart main.dart.
How do I make the import work? The error I got was:
Could not import "package:uuid/uuid.dart" from "file:///C:/folder/main.dart":
Could not resolve a package location for base at file:///C:/folder/main.dart
I already did a pub global activate uuid.
pubspec.yaml and pub get (or pub upgrade) are used to make a package available to your project at all.
Only after a package is available libraries from this package can be imported using a package URI.
pub global activate is to make packages available that contain executables to be executed from the command line.
See also https://www.dartlang.org/tools/pub/get-started

Dart web application can not import local package

I have developed a simple library (called picasawebalbums) that I wish to import in another web application.
I have added the the following to pubspect.yaml
picasawebalbums:
path: C:\Users\hangs_000\Documents\GitHub\PicasaWebAlbums
But the package does not appear under the 'packages' directory and of course my dart code cannot import any of the classes.
I feel that I have missed out a step somewhere.
Try pub get --verbose or pub upgrade --verbose from command line in your application directory and look at the output. You can try without --verbose first. This prints much less but may offer a hint anywhere.

Failed to load package in Dart

When I try to import a package with the syntax import 'package:markdown/markdown.dart';, I get no error in Dart Editor but when I run the dart application, the debugger shows me the message:
An error occurred loading file: package:markdown/markdown.dart
Failed to load resource
chrome-extension://gfjabgeipkcfopofkhjimepepnomcidk/dart/packages/markdown/markdown.dart
But when I write the whole path (import "../../packages/markdown/markdown.dart";) everything works fine. I cannot understand why the syntax package: doesn't work in my code though it works in Dart Editor's own examples.
You can see the Chrome app architecture below (I'm loading a package from translator.dart):
You should have the package added as a dependency in pubspec.yaml (which I assume you do).
Also try running following:
delete packages folder
delete pubspec.lock
run pub get to fetch the
dependencies again.
I think in your md_to_html folder you need a link to the 'packages' directory for the shortform to work. I seem to remember this happening when the workspace is built but I'm not sure now which command triggers it, have a look at the current pub docs.

Resources