【问题标题】:Flutter Error : A value of type 'Object?' can't be assigned to a variable of type 'String'颤振错误:“对象?”类型的值不能分配给“字符串”类型的变量
【发布时间】:2021-07-20 00:01:28
【问题描述】:

在颤振中使用滑块时出错。

错误所在的代码:

 Slider(
                    value: (_currentStrength ?? userData.strength).toDouble(),
                    activeColor:
                        Colors.brown[_currentStrength ?? userData.strength],
                    inactiveColor:
                        Colors.brown[_currentStrength ?? userData.strength],
                    min: 100.0,
                    max: 900.0,
                    divisions: 8,
                    onChanged: (val) =>
                        setState(() => _currentStrength = val.round()),
                  ),

用户数据类:

class UserData {

  final String uid;
  final String name;
  final String sugars;
  final int strength;

  UserData({ required this.uid, required this.sugars, required this.strength, required this.name });

}

ScreenShot

【问题讨论】:

    标签: android flutter mobile


    【解决方案1】:

    您正在使用启用了声音空值安全的 Flutter。该错误消息表示您正在尝试将可能为 null 的值分配给可能不为 null 的参数。

    您可以通过在传入的值中添加感叹号来解决您的问题,例如:_currentStrength ?? userData.strength!

    但请阅读:https://dart.dev/null-safety

    【讨论】:

    • 抱歉添加'!'不影响问题,但它说我可以删除'??'我不想删除的运算符。
    • 你能在声明 userData 的地方添加代码 sn-p 吗?问题似乎是strength 可以为空。
    猜你喜欢
    • 2021-06-19
    • 2021-09-27
    • 2020-07-02
    • 2021-03-28
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    相关资源
    最近更新 更多