-2

The argument type 'String?' can't be assigned to the parameter type 'String

code is here.kindly please solve my problem sir

 class IconContent extends StatelessWidget {
      IconContent({this.icon,this.label});
      final IconData? icon;
       final String? label;
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              icon,
              size: 80.0,
            ),
            SizedBox(
             height: 15.0,
            ),
            Text(
              label,
              style: TextStyle(
                fontSize: 18.0,
                color: Color(0xFFB2B58E),
              ),
            )
          ],
        );
      }
    }
  • Does this answer your question? ["The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()](https://stackoverflow.com/questions/66695339/the-argument-type-string-cant-be-assigned-to-the-parameter-type-string-w) – Tom Dec 20 '21 at 14:19

3 Answers3

3

Text widget doesn't take null value and in your snippet label is defined as nullable final String? label. It is possible to have null for label which violet the Text widget requirement.

const Text(
  String this.data, {
....})

@NirmalCode answer is ok, but I'm explaining to it why you need it.

You can provide default value while it will be null like Text(label ?? 'default',)

Or avoid rendering while label is null.

if(label!=null) Text(label!)

As you can see, I'm using bang! operator when I'm sure it will never get null. And you can also do it directly when you are certain about not getting null. I would say, in that case, don't make nullable label.

More about null-safety

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
2

String? is a nullable String which is not equal to String

More about null-safety: https://dart.dev/null-safety

Change,

IconContent({this.icon,this.label});
      final IconData? icon;
       final String? label;

To,

IconContent({this.icon, required this.label});
      final IconData? icon;
       final String label;

Or,

Change,

Text(
              label,
              style: TextStyle(
                fontSize: 18.0,
                color: Color(0xFFB2B58E),
              ),
            )

To,

if(label != null)
    Text(
        label!,
        style: TextStyle(
            fontSize: 18.0,
            color: Color(0xFFB2B58E),
        ),
    )

! indicates the label is not null. Make sure the label is never null if you use !

Or,

Text(
              label ?? 'default label incase label is null',
              style: TextStyle(
                fontSize: 18.0,
                color: Color(0xFFB2B58E),
              ),
            )
NirmalCode
  • 2,140
  • 1
  • 14
  • 19
-2

This is because your variable is nullable, you need to convert String? to String using toString()

class IconContent extends StatelessWidget {
      IconContent({this.icon,this.label});
      final IconData? icon;
       final String? label;
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              icon,
              size: 80.0,
            ),
            SizedBox(
             height: 15.0,
            ),
            Text(
              label.toString(),
              style: TextStyle(
                fontSize: 18.0,
                color: Color(0xFFB2B58E),
              ),
            )
          ],
        );
      }
    }

if you use label! enter image description here

if you use toString() enter image description here

Diwyansh
  • 2,961
  • 1
  • 7
  • 11