错误:(参数类型“String?”无法分配给参数类型“String”,因为“String?”可以为空,而“String”则不能。)在Flutter中

2023-12-24

我是 flutter 的新手,在传递字符串时遇到错误,并到处查找,最后将其添加到 StackOverflow 中

错误是

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't.

void errorSnackBar(BuildContext context, String? text) {
  ScaffoldMessenger.of(context).showSnackBar(new SnackBar(
    duration: new Duration(seconds: 4),
    content: new Row(
      children: <Widget>[
        Icon(
          Icons.error,
          color: Colors.red,
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(10.0, 0, 0, 0),
          child: new Text(text),
        ),
      ],
    ),
  ));
}

我有同样的问题

就我而言,我必须使用空安全。

final String? name

我不得不使用!标记以使其发挥作用。比如:如果我想称呼它

index.name!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:(参数类型“String?”无法分配给参数类型“String”,因为“String?”可以为空,而“String”则不能。)在Flutter中 的相关文章

随机推荐