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

Question

Asked by mahmudul h on December 20, 2021 (source).

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

Answer

Question answered by NirmalCode (source).

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),
              ),
            )
DART FLUTTER
SHARE: