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

Avi*_*Jha 2 dart android-studio visual-studio-code flutter flutter-layout

我是 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),
        ),
      ],
    ),
  ));
}
Run Code Online (Sandbox Code Playgroud)

sam*_*in 12

我有同样的问题

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

final String? name
Run Code Online (Sandbox Code Playgroud)

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

index.name!
Run Code Online (Sandbox Code Playgroud)