我在处理我的 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/