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
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
3
Answers
This means, category is nullable. So you could write instead
Text(widget.category ?? 'alt text')
what provides some alt text if category is null.You defined
category
as a nullable String. That is of typeString?
. AText
widget requires it to be non-nullable, which is of typeString
. There’s a couple of solutions:Make the
category
non-nullable. To do that changeString? category
toString category
. This might give other errors so it might not be possible for you to do.If you are sure it’s never
null
at that place in the code write a!
behind the variable name. LikeText(widget.category!)
. This will throw errors at runtime in case it is actuallynull
.You could also provide a fallback value in case it’s null, like
Text(widget.category ?? 'fallback')
. This is probably the safest solution.Change
widget.category
to
or