Search code examples
flutterandroid-studiodartvisual-studio-codeflutter-layout

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


I'm new to flutter and getting an Error in passing the String and looked everywhere and finally adding this in StackOverflow

the error is

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),
        ),
      ],
    ),
  ));
}

Solution

  • I had the same problem

    In my case, I had to use null safety.

    final String? name
    

    I had to use a ! mark to make it work. Such like: if I wanted to call it

    index.name!