【问题标题】:type 'String' is not a subtype of type 'FontStyle'“String”类型不是“FontStyle”类型的子类型
【发布时间】:2021-04-16 18:57:11
【问题描述】:

我试图将 FontStyle 值和 FontWeight 值存储在 Flutter 中的 HIVE 数据库中作为字符串值。 但是,在获取值时,我面临无法将 String 值转换为 FontStyle 或 FontWeight 值的问题。我可以使用 If 语句并实现它,但有没有办法将 String "FontStyle.italic" 转换为 FontStyle.italic?

【问题讨论】:

  • 是否有与这些类等效的整数?示例 FontWeight.w900 = 1001239(Some Integer) ? @JoãoSoares
  • 您的问题仅针对 FontStyle 吗?因为只有正常或斜体。你可以为它做一个简单的解决方案。

标签: string flutter dart fonts flutter-hive


【解决方案1】:

FontStyle 的类型为enum,因此默认情况下不能有整数等价。但是,由于FontStyle 只有两个常量名称normalitalic,因此您可以创建自己的整数等效项。例如,normal 为 0,italic 为 1。

示例:

// fontStyleInt is the int retreived from Hive (0 or 1)
int fontStyleInt;

// In the build
Text("Text", style: TextStyle(fontStyle: fontStyleInt == 0 ? FontStyle.normal : FontStyle.italic))

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 2021-02-05
    • 2021-06-28
    • 2021-01-14
    • 2021-11-07
    • 2020-12-14
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多