How is `flutter_localizations` imported into Flutter project when it's not available on pub.dev? - flutter

I am wondering how this is possible. The flutter_localizations package is not on pub.dev which makes me think that it's part of flutter package? But if so, how come I have to import it like package:flutter_localizatoins/flutter_localizations.dart into a project?

Check out the official docs
TLDR: In your pubspec.yaml, is not added like a regular dependency
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
It works the same way as flutter(it comes bundled with the sdk itself) which you also import its packages like:
import 'package:flutter/material.dart'

To use localized messages configured with intl package (this is the way from official docs) add this import:
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
It will be generated during build.
Use messages this way:
final localizations = AppLocalizations.of(context)!;
print(localizations.hello);
For more details see docs here.

Related

The library 'package:vin_decoder/vin_decoder.dart' is legacy, and should not be imported into a null safe library

I am trying to use a vin decoder package in my app. When I import the package:
import 'package:vin_decoder/vin_decoder.dart';
but I am getting this warning:
The library 'package:vin_decoder/vin_decoder.dart' is legacy, and should not be imported into a null safe library.
Try migrating the imported library.
My pubspec.yaml has this under dependencies:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
vin_decoder: ^0.1.4
Can someone explain what I am doing wrong? I checked out this stack overflow question but nothing worked.
Thank you.
You are doing nothing wrong. Currently this package has not released a null safety version yet, and you project is null safe. Check package, there is a prerelease version 0.2.1-nullsafety. Either wait for it to be released, or use this prerelease version, but of course it can contain bugs.

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)

how to "Migrate the plugin to the pubspec platforms manifest"

I have created a new flutter app and after adding cloud_firestore 0.13.0+1 it will not compile. No errors it just stops and removes the row of compile icons. An Analysing spinner revolves forever.
In pub.dev it says to "Migrate the plugin to the pubspec platforms manifest." unfortunately I have been unable to find out where that is or how to do it in the documents or StackOverflow. Please help.
According to documentation, the plugins you required need to be added under dependencies in to a pubsec.yaml file located in the root of your project.
dependencies:
cloud_firestore: ^0.13.0+1
After your plugins are added to pubsec.yaml, you can make use of those dependencies in your code by using imports.
import 'package:cloud_firestore/cloud_firestore.dart';

Flutter | Dart : Target of URI does not exist

I am making my first Application to build an Android App with Flutter.
I am using Android Studio as IDE.
The problem is when I import the http package:
import 'package:http/http.dart' as http;
I get an error :
error: Target of URI doesn't exist: 'package:http/http.dart'.
(uri_does_not_exist at [flutter_crypto] lib\home_page.dart:3)
That's my code :
Future<List> getCurrencies() async{
String cryptoUrl = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
http.Response response = await http.get(cryptoUrl);
return JSON.decode(response.body);
}
Thanks,
You need to add the HTTP dependency to pubspec.yaml as per below.
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
http: ^0.12.0
With the dependency added you then need to run the following command to update/install required packages:
flutter packages upgrade
Hope this helps
Make sure you have added the dependency to pubspec.yaml
dependencies:
http: ^0.12.0
You will also need to:
flutter packages get
For any upcoming problem with importing packages from dart please go to official Dart website packages and search for needed package and you will find solution up there.
for your issue go to:
https://pub.dartlang.org/packages/http
Installing tap https://pub.dartlang.org/packages/http#-installing-tab-
follow guide steps:
In pubspec.yaml file:# dependencies
dependencies:
http: ^0.12.0
flutter:
(NB: please make sure that http & flutter or any other attribute inside dependencies are aligned as above and they are at same line)
In terminal run below command:
$flutter packages get
run: dart pub --trace get --no-precompile in android srudio Terminal

How to import intl library in Flutter?

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;
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/
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.
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';
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