skip to Main Content

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


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. Change widget.category
    to

    widget?.category: ""  👈 Prefer this else you'll get null operator on null value error
    

    or

     widget!.category
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search