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

Time:01-06

child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( widget.category, //here )

I want to display the category where i call it from database but the Text fill occur an error. Please help me

CodePudding user response:

You defined category as a nullable String. That is of type String?. A Text widget requires it to be non-nullable, which is of type String. There's a couple of solutions:

  1. Make the category non-nullable. To do that change String? category to String category. This might give other errors so it might not be possible for you to do.

  2. If you are sure it's never null at that place in the code write a ! behind the variable name. Like Text(widget.category!). This will throw errors at runtime in case it is actually null.

  3. You could also provide a fallback value in case it's null, like Text(widget.category ?? 'fallback'). This is probably the safest solution.

CodePudding user response:

This means, category is nullable. So you could write instead Text(widget.category ?? 'alt text') what provides some alt text if category is null.

CodePudding user response:

Change widget.category to

widget?.category: ""             
  • Related