Home > Enterprise >  Google font family not working in flutter web
Google font family not working in flutter web

Time:08-07

In my flutter web application I and have a functionality to let user select font family for some texts. The text selection panel uses google fonts. And when I send the updated values to server I am sending the font family as String. Everything works perfectly fine until now.

But when I try to show the updated text(with the newly selected font family) I get 'font_family' not found exception.

  TextStyle _textStyle() {
    return TextStyle(
      fontFamily: GoogleFonts.getFont(widget.item.googleFontFamily).fontFamily!,
      fontSize: widget.item.fontSize,
      fontWeight: widget.item.fontWeight,
      foreground: Paint()
        ..style = widget.item.strokeWidth > 0
            ? PaintingStyle.stroke
            : PaintingStyle.fill
        ..strokeWidth = widget.item.strokeWidth
        ..color = widget.item.textColor.withOpacity(widget.item.textOpacity),
      letterSpacing: widget.item.letterSpacing,
      fontStyle: widget.item.italic ? FontStyle.italic : FontStyle.normal,
    );
  }

widget.item.googleFontFamily is a string.

Steps I tried:

  1. I tried hard coding the font family like: GoogleFonts.getFont('AbrilFatface_regular').fontFamily!; still getting the issue.

CodePudding user response:

Add the space to the name.

GoogleFonts.getFont('Abril Fatface').fontFamily

CodePudding user response:

The problem was elsewhere, I was setting the font family as:

widget.item.googleFontFamily = GoogleFonts.getFont(value).fontFamily

This was converting a font family like Abril Fatface to Abril_Fatface_regular which was later showing up as font family not found.

I simply had to set the fontFamily as:

widget.item.googleFontFamily = value

Which solved the issue.

  • Related