How to import intl library in Flutter?
Asked Answered
P

4

16

I am new in Flutter. When I import the library: import 'package:intl/intl.dart'; , it says that the target of URI doesn't exist:package:intl/intl.dart; enter image description here

Pinhole answered 6/8, 2018 at 10:37 Comment(7)
Just to double check, you did import intl: ^0.15.7 into pubspec.yaml; triple check that it has four spaces in front of it (no more and no less); and you ran packages get? Also, put your focus on the tab for main.dart and hit the green arrow to run it. Sometimes you will then see a popup bar at the top of the screen that tells you pubspec.yaml has changed and you need to run it again from that link in order for it to take. (I've seen that in IntelliJ) Let us know if that doesn't help.Scutage
It works now. Thanks! But it is still showing the red underlines. Is there a way to solve it ? @ScutagePinhole
If it's showing red lines under publspec.yaml in the project window but everything is working, that's a bug in the analysis. Ignore it but yes, they do know about it and are working on it. It's often there because, for some reason, pubspec.yaml says your assets directory doesn't exist even though you can access the assets without any problem.Scutage
@Scutage No it is showing under main.dart (as shown in the picture in my question)Pinhole
Try stopping the app, going to the terminal and running "flutter clean" Also, there is a chance that you might have a second import that is trying to use a different class with the same name, but try the clean first.Scutage
What does Dart Analysis say when you check it? (Bottom bar in IntelliJ)Scutage
Problem solved! I flutter clean didn't do anything. I just restarted IntelliJ and red lines are gonePinhole
S
16

Just to double check, you did import intl: ^0.15.7 into pubspec.yaml; triple check that it has four spaces in front of it (no more and no less); and you ran packages get?

Also, put your focus on the tab for main.dart and hit the green arrow to run it. Sometimes you will then see a popup bar at the top of the screen that tells you pubspec.yaml has changed and you need to run it again from that link in order for it to take. (I've seen that in IntelliJ)

Also, if it's showing red lines under publspec.yaml in the project window but everything is working, that's a bug in the analysis. Ignore it but yes, they do know about it and are working on it. It's often there because, for some reason, pubspec.yaml says your assets directory doesn't exist even though you can access the assets without any problem.

Scutage answered 6/8, 2018 at 10:49 Comment(0)
K
30

When you import any package, example:

import 'package:intl/intl.dart';

You need to also add the package inside the pubspec.yaml file under the dependencies field example:

dependencies:
  intl: ^0.15.7

Then from the terminal you can execute the following command:

flutter packages get

or

From Android Studio/IntelliJ:

Click Packages Get in the action ribbon at the top of pubspec.yaml

more info here:

https://flutter.io/using-packages/

Kinch answered 6/8, 2018 at 11:30 Comment(0)
S
16

Just to double check, you did import intl: ^0.15.7 into pubspec.yaml; triple check that it has four spaces in front of it (no more and no less); and you ran packages get?

Also, put your focus on the tab for main.dart and hit the green arrow to run it. Sometimes you will then see a popup bar at the top of the screen that tells you pubspec.yaml has changed and you need to run it again from that link in order for it to take. (I've seen that in IntelliJ)

Also, if it's showing red lines under publspec.yaml in the project window but everything is working, that's a bug in the analysis. Ignore it but yes, they do know about it and are working on it. It's often there because, for some reason, pubspec.yaml says your assets directory doesn't exist even though you can access the assets without any problem.

Scutage answered 6/8, 2018 at 10:49 Comment(0)
T
6

Let IDE do this for you :

flutter pub add intl

After running above command, It will resolve the dependency with the latest version available.

OR Manual Process

1) Add package in pubspec.yaml file under dependencies field :

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  intl: ^0.17.0   // Add this line

2) Execute the following command in terminal :

flutter packages get

3) Import package in your dart file :

import 'package:intl/intl.dart';
Torrefy answered 6/6, 2021 at 12:45 Comment(1)
Sorry for the necro but... Bro, the 'flutter pub add' line, so much easier. Imma +1 this.Extravagant
B
-1

if you are facing errors while installing it coming because of your old version of SDK.

Just fall back to the intl version back by some points in your pubs intl: ^0.17.0 ---> intl: ^0.16.1 or any other older version like intl: ^0.15.1 etc. Sometimes its the version of packages which after update not compatible with your old sdk

Boondoggle answered 14/3, 2021 at 3:5 Comment(1)
I don't know why they negativate, this tip worked for me. Thanks. You basically needs to install an older version and then upgrade to a new one.Bedside

© 2022 - 2024 — McMap. All rights reserved.