I didn't get any error in normal flutter run, but the apk crashes with the following error:
E/flutter (18669): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size!
E/flutter (18669):
F/flutter (18669): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
I have tried all the solutions I found online:
uses-material-design: true
pubspec.yaml snippet:
fonts:
- family: Open Sans
fonts:
- asset: Open Sans/OpenSans-Regular.ttf
main.dart snippet:
theme: ThemeData(
primarySwatch: MaterialColor(0xFF8c7ae6, primarySwatch),
primaryColor: Color(0xFF8c7ae6),
brightness: Brightness.light,
fontFamily: 'Open Sans', // using font
textTheme: TextTheme(button: TextStyle(fontWeight: FontWeight.w400)),
appBarTheme: AppBarTheme(
brightness: Brightness.light,
iconTheme: IconThemeData(color: Color(0xFF8c7ae6))),
primaryColorLight: Color(0xFF9c88ff)),
Font file is in a folder named "Open Sans" at project level.
right now we always use the system default font, And adding custom fonts is very easy in Flutter apps. First of all, you need to create a new folder in your project folder. So not in the 'lib' folder, but simply in your main project folder.
You can name that folder whatever you want, typically you. It's something like 'assets' or you directly name it 'fonts' whatever you want, I'll name it assets, and in the assets folder, I'll create a 'fonts' folder. I want to have it here in the assets folder.
Now there I want to use a couple of fonts and you find a zip file attached which includes the fonts, I will use here for example download from "https://fonts.google.com/download?family=Open%20Sans"
for example I brought in two fonts here, OpenSans in bold and the regular version and Quicksand in a bold light medium and regular version.
Now with the fonts added here, we still can't automatically use them in our application. To unlock them in our application and to include them in the app file that Flutter generates the end. You need to go to your 'pubspec.yaml' file, which again Is your global management tool here and there if you scroll down you should actually find this commented out area which already gives you an example of how to add fonts. So you can simply comment this back in by removing the hash symbols.
And then you see that you have fonts which is indented by two spaces and again in yaml files, spacing matters and their you define a 'family' key after the dash and a white space and that is the name of the font, this I'm here should match the name of your font, but theoretically it's up to you.
for example in file pubspec.yml :
fonts:
- family: OpenSans
fonts:
- asset: assets/fonts/OpenSans-Bold.ttf
- asset: assets/fonts/OpenSans-Regular.ttf
weight: 700
how use from fonts:
for example in file main.dart :
return MaterialApp(
title: 'Personal Expenses',
theme: ThemeData(
primarySwatch: Colors.purple,
fontFamily: 'OpenSans'
),
home: const MyHomePage(),
);