Google Fonts package Not working in flutter - flutter

The google_fonts package is not working in the final build apk in FLutter, it works fine in the debug mode, but as soon as I build it and install the final apk, It just shows regular font.

It could be because of internet permission issue, google fonts using internet acces and if don't apply the permission in your project it won't work when you install the app.
for this you need to add follow line to file AndroidManifest.xml which is there;
yourappname/android/app/src/debug/AndroidManifest.xml
and the line you will add:
<uses-permission android:name="android.permission.INTERNET"/>

I don't believe it is an internet problem. Most times fonts using Flutter packages contain an error in the Pubspec file.
Check your Pubspec.yaml file. Make sure your google fonts package is Formatted this way
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
google_fonts: ^1.1.0
flutter:
assets:
- google_fonts/ # you only need this line and nothing in the fonts area per https://pub.dev/packages/google_fonts

Make sure you have added the below line in /android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>

hello my friend try to check the version first ;)
enter image description here

To use google_fonts you must initialize the license for it.
To do that follow the steps:
Suppose you using this:
fontFamily: GoogleFonts.shadowsIntoLight().fontFamily,
Therefore, you are using shadowsIntoLight font.
To license it, first download the particular font from Google Fonts.
After you download the zip files, extract it.
Make a folder named google_fonts in the root directory of your flutter app file.
=> Now place the .ttf and OFL.txt files (present in the extracted folder) in google_fonts folder that you made previously.
[Note: Only OFL.txt is enough for all your fonts.]
This is how my one looks:
Now go to main.dart file:
=> Add these lines inside the void main() {runApp(MyApp());}:
void main() {
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('google_fonts/OFL.txt');
yield LicenseEntryWithLineBreaks(['google_fonts'], license);
});
runApp(...);
}
and import the followings:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Now close your app and restart it and the release it:
flutter build apk --release

Google fonts uses internet, so you may have to update the dependencies in the manifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Else you can try to make the google font available in the offline by adding it in the assets folder.
And then change the code from
// Online Code
Text(
'This is hammersmithOne from Google Font'
style: GoogleFonts.hammersmithOne(),
),
to
// Offline Code
Text(
'This is hammersmithOne from Google Font',
style: TextStyle(fontFamily: 'hammersmithOne') // This is loaded from assets
),
For futher reference refer this article.

Related

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)

packages get not fetching .packages for English Words with proper pubspec.yaml

for context I am currently following this exercise provided by flutter dev: https://codelabs.developers.google.com/codelabs/first-flutter-app-pt1/#3
I use android studio on a Windows 10 profesional.
I have seen this issue across many different areas and I am continually unable to get the English_Words package to upload. I had even uploaded the file directly the .packages and had the import work, however, the functions and classes do not validate
This is the import from main.dart
This is the pubspec.yaml, with the dependency properly spelled out
I had navigated and manually put in the english_words dependency. However, then the methods would not engage, even when the import worked. Furthermore, when I did ''' -flutter packages get ''', the file was removed, even with the .yaml dependency spelled out.
I have done lots of the recommended things:
-flutter doctor
-deleting the pubspec.lock and upgrading/getting packages
-shutting down the virtualization and android studio
cupertino icon and english words must have have a spacing of 2 just like flutter
ex:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
english_words:^3.1.5
this is a YAML file so spacing have a special meaning. just correct it as i did in example enjoy.

What is packages in Flutter assets inside pubspec.yaml file?

I'm using flutter gallery sample code, and found following in pubspec.yaml file.
flutter:
assets:
- packages/shrine_images/0-0.jpg
I tried to find out packages and 0-0.jpg file in the project but couldn't find. Can anyone tell me what this package is all about and from where I am seeing images when running the app?
Actually they have added a package
flutter_gallery_assets: 0.1.9+2
Check it in pubspec.yaml dependencies, its a package so that you can use images from this package. So the pictures are from that package and they are just mentioning the image path from that package. Here is the package link they are using click here

The Google Fonts Package in Flutter app is not working

First, I added the google_fonts package to your pubspec dependencies.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
Then
import 'package:google_fonts/google_fonts.dart';
and apply to a Text widget
Text(
'This is Google Fonts',
style: GoogleFonts.lato(fontSize: 40),
),
Text(
'This is Google Fonts',
style: GoogleFonts.adventPro(fontSize: 40),
),
Please check internet connection- your emulator does not have internet connectivity. google fonts need internet connection on device/emulator.
After adding the dependency pubspec.yaml file
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
google_fonts: ^0.2.0
run the command in terminal\cmd console as :
> flutter packages get
this will fetch the dependency into your workspace.
Google Fonts are fetched from the internet in the runtime. So you observe this behaviour
To overcome this, download the font from google font and make it available in the asset folder by following the steps below.
Visit the https://fonts.google.com/ and download the lato font.
At the root directory, create a directory called google_fonts.
Copy-Paste lato.ttf file into the google_fonts folder.
Open the pubspec.yaml file, Under the assets: section add the -google_fonts/
And change the code from
// Online Mode
Text(
'This is hammersmithOne from Google Font'
style: GoogleFonts.lato(),
),
to
// Offline Mode
Text(
'This is hammersmithOne from Google Font',
style: TextStyle(fontFamily: 'lato') // This is loaded from assets
),
For futher reference refer this article.
Refer these stackoverflow answers aswell:
https://stackoverflow.com/a/74949875/13431819
https://stackoverflow.com/a/74950261/13431819
I fixed mine by deleting the emulator and installing a new one. My older emulator was not connecting to the internet for some reason and reinstalling it fixed it for me.
Unless you store the fonts in your assets, you need to define explicitly that runtime fetching is enabled for Google Fonts:
GoogleFonts.config.allowRuntimeFetching = true;

Target of URI doesn't exist 'package:english_words/english_words.dart'

I'm learning to use Flutter using Android Studio. I'm going through step by step doc provided by Flutter. At Step:2 Use an external package I'm getting issue importing the english_words package. I have properly added the package in pubspec.yaml and clicked Packages Get which added the dependency but at the time of importing the package in lib/main.dart it is saying
Target of URI doesn't exist
'package:english_words/english_words.dart'.
I have seen many questions on StackOverflow but none of them helped me. Please help!
For some packages, once you do all the process described by the other answers to this question, you have to close the Android project and open it again. As well as the emulator.
Some packages need the restart, others do not need it.
After adding the package in the pubspec.yaml file, you need to execute the command flutter packages get or click on "Packages Get" in the action ribbon at the top of pubspec.yaml file.
Then the dependency and any transitive dependency will be added to the .packages file.
Check this:
https://flutter.io/using-packages/
Solution (For VSCode):
Run flutter packages get in pubspec.yml
Restart VSCode
I had the same issue and went about it like listed below and it worked.
1. Add the package
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
**english_words: ^3.1.5**
Save the pubspec.yaml file
go to your terminal and run 'flutter packages get'
4.Close your simulator and rebuild your cradle
Run the main.dart again and it should remove the error.
This is what worked for me, all the best.
Make sure that english_words is on the base-line with flutter:
Took me a lot of time, but in summary, just save the file pubspec.yaml will do. The command flutter pub get will be ran once u save it, then u can import the package.
There is no need to restart ur editor etc.
My solution was to add the "english_words: ^3.1.5" the version at the moment that I write this, under the "dependencies:" and before the "Flutter:" argument, without the "+" sign.
Hope that helps
I used this and it worked for me... without + sign and version in double quote ""..
english_words: "^3.1.5"
Make Sure that the alignment of english_words is correct inside your pubspec.yaml as it's "space sensitive" :
dependencies:
english_words: ^3.1.5 //two spaces
also never align it using tabs, I don't know why but it never works, and don't use + sign
There are so many methods to solve this issue, most of them will get solved on the second step,if not please try third method also it will work.
Run flutter packages get in pubsec.yaml file (if you are using VS Code it will be easier).
Restart your IDE.
Check it now, mostly it will work. If not try third step.
you have to repair pub caches
flutter packages pub cache repair
I hope it will work. if all methods are not working try to re install flutter.
my friend solution is so simple only add this code line in file pubspec.yaml
english_words: ^3.1.0
like this
dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
english_words: ^3.1.0
dev_dependencies:
flutter_test:
sdk: flutter
it would work perfectly:)
I faced the same issue but I managed to solve it using the steps below:
add the package as an example english_words: ^3.1.0" in 'pubspec.yaml file.
save file or press ctrl+s
//the Output Console will appear this message
[app name] flutter packages get.
Running "flutter pub get" in first_app..., 2.4s
exit code 0.).
3. check main.dart file, the error will be removed.
For me nothing worked. Things resolved only after
Flutter upgrade
in pubsec.yaml file
For those who are still experiencing this problem, it took me hours to figure out what was wrong. Basically, if all the other answers don't work:
Open the test/.packages file on whatever IDE you are using
Locate the package that is not working and go to that directory on your computer.
Delete the whole package. In my case it was the whole english_words package folder.
In the pubspec.lock and .packages file, delete all instances of that package.
run pub get
In the docs of https://flutter.dev/docs/get-started/codelab there is the following code:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
+ english_words: ^3.1.5
The thing which is causing the error is "+ sign". I just removed it and all worked fine for me.
Delete the plus sign "+" in front of english_words (if you have one)
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
english_words: ^3.1.5
I got the same issue.
It is observed only in version 4.0.0 .For lower versions there is no problem.
So here is what you can do to solve this:
Download code for the dependency from :
https://github.com/filiph/english_words
Unzip it Rename it to
english_words-4.0.0
Copy this folder.
Now go to flutter's SDK folder and navigate to
.pub-cache\hosted\pub.dartlang.org
and paste the folder english_words-4.0.0 here. Add dependency in pubspec.yaml file. Now run flutter pub get command.
You are good to go.
Note : How to find the path of Flutter SDK
All you need to do is to restart IDE(Android studio or VSCode)
Having this error message:
Error: Cannot run with sound null safety, because the following
dependencies don't support null safety:
package:english_words
I had to change this dependency english_words: ^3.1.5 to the latest version english_words: ^4.0.0 and make "pub get":
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
english_words: ^4.0.0
provider: ^6.0.0
in your pubspec.yaml file, instead of:
english_words: ^4.0.0
try:
english_words: '4.0.0'
it worked for me