flutter - 'TextStyle ?' can' t 被分配给参数类型 'TextStyle'

标签 flutter dart

我在处理我的 flutter 应用程序时遇到了这个空安全错误。

The argument type 'TextStyle?' can't be assigned to the parameter type 'TextStyle'.
这是引发此错误的代码片段:
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button))
VS Code 建议此解决方案将 not 运算符放在参数的末尾。
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button!))
问题:这是使用 的好习惯吗? ! 在我的代码中以及在这种情况下还有哪些其他解决方案?

最佳答案

Material 状态属性表示取决于小部件的 Material “状态”的值。状态被编码为一组 MaterialState 值,例如 MaterialState.focused、MaterialState.hovered、MaterialState.pressed。例如, InkWell.overlayColor 定义了在按下(“初始颜色”)、聚焦或悬停时填充墨水的颜色。 InkWell 使用叠加颜色的解析方法来计算墨水池当前状态的颜色。
您只能使用上下文主题来使用主题 textStyle ,如下所示:

Text("copied text theme",
 textAlign: TextAlign.center,
 style: Theme.of(context).textTheme.button)

关于flutter - 'TextStyle ?' can' t 被分配给参数类型 'TextStyle',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66273180/

相关文章:

Flutter从DropDropdownButton删除底部的下划线

bash - 安装 Flutter 后从 Mac OS 终端运行 Dart 程序

flutter - 如何将继承的小部件传递给整个 Material 应用程序

firebase - 错误 :flutter/lib/ui/ui_dart_state. cc(177) 未处理的异常:NoSuchMethodError:在 null 上调用了方法 'insert'

flutter - 绘制一个带边框的椭圆来显示图像

dart - 我无法添加 Text(installedApps[index] ["app_name"]) 因为 'index' 未定义。我怎样才能添加这个文本?

flutter - 放大数据存储 : How to know when sync is finished?

firebase - Flutter:Firebase身份验证无需登录即可创建用户

dart - 是否可以将 Flutter 应用注册为 Android Intent 过滤器并处理传入的 Intent?

dart - Flutter:Facebook 和 Google 身份验证