【问题标题】:Invalid Constant Value Text Flutter Google Fonts无效的常量值文本颤振谷歌字体
【发布时间】:2022-01-14 03:18:00
【问题描述】:

我正在使用 Google 字体设计的 Text,并且工作正常,但是当我在 Text 之前添加 const 时,我得到 Invalid Constant Value Error

下面是我在没有 const 的情况下是如何实现的


Text(
    'Voila',
     style: GoogleFonts.dawningOfANewDay(fontSize: 30),
),

下面是我尝试用 const 实现的方法


const Text(
        'Voila',
         style: GoogleFonts.dawningOfANewDay(fontSize: 30),
),

以下是我如何尝试使 google 字体保持不变,但错误仍然存​​在


const Text(
              'Voila',
              style: const GoogleFonts.dawningOfANewDay(fontSize: 30),
            )

【问题讨论】:

    标签: flutter google-fonts flutter-text


    【解决方案1】:

    您必须从您的代码中删除const,因为constant values 应该是hard coded,但是您使用的字体样式或字体系列动态地变为const 不允许这样做。

    Text(
             'Voila',
             style: const GoogleFonts.dawningOfANewDay(fontSize: 30),
         )
    

    【讨论】:

    • 所以现在我可以使用 GoogleFonts 作为常量
    • 为什么不只需将该字体系列作为资产在本地存储,然后您可以将该字体系列与 const 一起使用。
    • 好的,我会试试的
    • 好的,尝试后如果有效,然后将此问题标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 2020-07-19
    • 2021-11-24
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多