Raj Dhakad
Raj Dhakad

Reputation: 932

Flutter: [Fatal Error] Can't use custom Fonts

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:

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.

Upvotes: 0

Views: 2201

Answers (4)

alireza jalilian
alireza jalilian

Reputation: 161

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(),
    );

Upvotes: 1

Aknahseh_tg
Aknahseh_tg

Reputation: 175

I went through this error sometime back with my colleague recently, still not sure what exactly fixes it...But it was fixed for colleague after reinstalling the flutter sdk [which would kind of make sense 'cause of cpp file] and for me after upgrading my flutter version..

(This is given that you have already tried Flut0101 's answer BTW as it was a common issue)

Upvotes: 0

Flut0101
Flut0101

Reputation: 21

Remove the space between 'Open Sans' so that it's 'OpenSans' everywhere.

Upvotes: 2

Ayush Bherwani
Ayush Bherwani

Reputation: 2719

You can use google_fonts package to get all the fonts available at Google Fonts.

Add the google_fonts package to your pubspec dependencies.

dependencies:
  flutter:
    sdk: flutter
  google_fonts:

Import the google fonts package in the main.dart

To use GoogleFonts

ThemeData(
    textTheme: GoogleFonts.openSansTextTheme(
      Theme.of(context).textTheme,
    ),
  )

Upvotes: 0

Related Questions