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;
Related
Instead of seeing my image in my app I'm seeing a red box with an X that says Unable to load asset: images/aboutmaggie.png.
I made a directory assets with a subdirectory images. The directory assets is at the same level as pubspec.yaml.
I put the image into the images directory. When I click on the image in Android Studio the image displays.
In pubspec.yaml I have these lines:
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:
- assets/images/
I added
class AboutRoute extends StatelessWidget {
const AboutRoute({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Kabbalistic Numerology'),
),
body: ListView(
shrinkWrap: true,
padding: const EdgeInsets.all(8),
children: <Widget>[
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: 'About Maggie McPherson',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
)),
],
),
textAlign: TextAlign.center,
),
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text:
"Dr. Leslie Margaret Perrin McPherson...",
style: TextStyle(
color: Colors.black,
)),
],
),
),
Image.asset('images/aboutmaggie.png'), // <--this image doesn't load
]));
}
}
I ran flutter pub get. I ran Tools > Flutter > Flutter Clean. I shut down and restarted Android Studio.
The error message is:
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/aboutmaggie.png
I tried putting another image into assets/images and calling that but the second image wouldn't load either.
What's wrong with this picture?
there is an error in calling the image,
change this line in your code
Image.asset('images/aboutmaggie.png')
to this line
Image.asset('assets/images/aboutmaggie.png')
I hope this helps, thankyou
Try like this:
Image.asset('assets/images/aboutmaggie.png'),
Try below code hope its helpful to you.
Refer documation here for Adding assets and images
Refer Image Class here
Your folder structure
project directory
- assets
- images
- your_image.png
Your pubspec.yaml file
flutter:
assets:
- assets/images/
Your Widget:
Image.asset(
'assets/images/ln.png',//just change your image to my image
height: 150,
//width: 100,
),
Note - If you adding correct code and details then your problem not resolved then stop(exit) the project/app and relaunch again
Result Screen->
You need to provide complete path of the image.
Replace
Image.asset('images/aboutmaggie.png')
with
Image.asset('assets/images/aboutmaggie.png').
It was cacheing issue. Last night I tried
Image.asset('assets/images/aboutmaggie.png'),
but that didn't work. This morning I started up Android Studio and it asked,
pubspec.yaml has changed. Do you want to run flutter pub get?
Well, that's odd. I'd just started Android Studio. I hadn't changed anything. I changed the path back to 'assets/images/aboutmaggie.png', ran flutter pub get, did a hot restart and the image appeared.
Then I opened pubspec.yaml and changed it to
assets:
- assets/bowlingballs/
I ran flutter clean, flutter pub get, did another hot restart, and the image continued to appear.
I changed the path back to 'images/aboutmaggie.png', ran flutter pub get, did a hot restart and the image was gone.
My conclusion is that if you change this path
Image.asset('assets/images/aboutmaggie.png'),
then run flutter pub get and a hot restart then the change will appear. But if you make changes to pubspec.yaml you won't see changes until some cache is cleared. The problem I had last night was that when I set up pubspec.yaml I had an extra space before assets. That version of pubspec.yaml got cached and subsequent versions of pubspec.yaml were ignored. flutter clean clears the Build cache but pubspec.yaml apparently isn't in the Build cache. pubspec.yaml must be cached somewhere else.
In your terminal, run flutter clean to clear up the cache, then run flutter pub get for flutter to fetch your assets folder afresh
This is what worked for me
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.
I have a package named Handwriter. It writes text in a custom font. I have the .ttf saved in lib/third_party/. In its pubspec.yaml, I add the font:
flutter:
uses-material-design: true
fonts:
- family: FancyHandwriting
fonts:
- asset: lib/third_party/FancyHandwriting-Regular.ttf
My app imports this package. In its pubspec.yaml, I add Handwriter as a dependency:
dependencies:
flutter:
sdk: flutter
handwriter:
path: ../handwriter
model:
path: ../model
However, the font does not show up at all when I use it in my app. Why?
final defaultStyle = TextStyle(
fontFamily: 'FancyHandwriting',
fontSize: 130);
Without having to re-add the fonts in the package consumer app, you can use:
final defaultStyle = TextStyle(
fontFamily: 'FancyHandwriting',
package: 'handwriter',
fontSize: 130,
);
Which is also equivalent to doing:
final defaultStyle = TextStyle(
fontFamily: 'packages/handwriter/FancyHandwriting',
fontSize: 130,
);
To use your Flutter Icon font from a custom package, you can follow these steps:
create a flutter icon font
You can use flutter icon for that https://www.fluttericon.com
Put the file into your package
Make sure to copy the .ttf file into of the /lib folder.
NOT just assets, as you would do in your root project.
Example path:
/packages/my_awesome_fontpackage/lib/assets/MyIconFont.ttf
(see https://zubairehman.medium.com/how-to-use-custom-fonts-images-in-flutter-package-c2d9d4bfd47a )
Add the font to your project
Now open your package's pubspec.yaml file and add the font as an asset with package path:
flutter:
fonts:
- family: MyIconFont
fonts:
- asset: packages/my_awesome_fontpackage/assets/MyIconFont.ttf
(you might have to restart your app and bundling completely for the font to be loaded correctly)
Now add package declaration to font dart file
Go into the my_icon_font.dart file and change the constant _kFontPkg there to your package name.
class MyIconFont {
MyIconFont._();
static const _kFontFam = 'MyIconFont';
static const String? _kFontPkg = 'my_awesome_fontpackage';
static const IconData bell = ...
....
}
According to this: https://flutter.dev/docs/cookbook/design/package-fonts you have to declare the package's font in your app's pubspec.yaml:
Declare the font assets
Now that you’ve imported the package, tell
Flutter where to find the fonts from the awesome_package.
To declare package fonts, prefix the path to the font with
packages/awesome_package. This tells Flutter to look in the lib folder
of the package for the font.
I.e.:
dependencies:
flutter:
sdk: flutter
handwriter:
path: ../handwriter
model:
path: ../model
flutter:
uses-material-design: true
fonts:
- family: FancyHandwriting
fonts:
- asset: packages/handwriter/third_party/FancyHandwriting-Regular.ttf
Note you're declaring the asset as being from the package - you don't have to make a copy of it in your app's lib.
The reasoning behind this is that not all fonts from every package may be used, so this trims down your final app's size.
Don't forget that you must also import/copy the fonts into your font assets directory. In your case make sure you place them in lib/third_party/. (See step 1 - https://flutter.dev/docs/cookbook/design/fonts).
I'm currently working on a side project and when I have included a font-awesome icon inside my app, it does not show up and instead, a blank box with two lines intersecting with each other like the letter x, how to fix this?
class ImageHoldingComponent extends StatefulWidget {
#override
_ImageHoldingComponentState createState() =>
_ImageHoldingComponentState();
}
class _ImageHoldingComponentState extends State<ImageHoldingComponent> {
#override
Widget build(BuildContext context) {
return Container(
child: Icon(FontAwesomeIcons.python)
);
}
}
The author of font_awesome_flutter package suggests the following steps for anyone who has icons not showing problem:
Stopping the app
Running flutter clean in your app directory
Deleting the app from your simulator / emulator / device
Rebuild & Deploy the app.
Also make sure to set uses-material-design to true in pubspec.yaml file.
This happens when you start using the older version of font_awesome_flutter and because the virtual device cache is stored when you build the Flutter app, so every time you change the icon you have to stop the emulator and rebuild it.
My experience is:
Stop main.dart (Android Emulator)
In pubspec.yaml add (font_awesome_flutter) new version
font_awesome_flutter: ^8.8.1
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
font_awesome_flutter: ^8.8.1 //new verison
And Then click
"Pub get"
at the top of Flutter commands
Run the emulator again (start main.dart)
If your app is running on the emulator or device just
stop it and relaunch it again
it solves your problem
Just save the from pupspec.yaml file under dependices and say pub get, don't forget to import it
Add font_awesome_flutter: ^10.3.0 package in pubspec.yaml file.
Then use like this
FaIcon(
Icons.access_alarm,
color: whatsappColor,
size: 30,
),
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