skip to Main Content

When trying to run my app, I get this error: The following assertion was thrown building WallPost: Cannot provide both a color and a decoration To provide both, use "decoration: BoxDecoration(color: color)". 'package:flutter/src/widgets/container.dart': Failed assertion: line 269 pos 15: 'color == null || decoration == null'

The code I used that resulted in the error was the following: Container(decoration:BoxDecoration(shape: BoxShape.rectangle, color: Colors.lightGreen ), child: const Icon(Icons.person), color: Colors.grey, ),

Everywhere I see tells me to just write the code as Container(decoration:BoxDecoration(color: color ) with the "color" parameter inside the decoration and not before it, but that’s exactly what I did and I’m still getting this error.

When I run the app, it gives me a red screen, saying:

'package:flutter/src/widgets/container.dart':failed assertion; line 269 pos 15: 'color == null

decoration == null’: Cannot provide both a color and a decoration
To provide both, use "decoration:BoxDecoration(color: color )".

which is the same code I’m already using.

2

Answers


  1. A container cannot have both the color property as not null and the color property of the decoration as not null. You will have to provide one. Either you remove the color from the container or remove the color from the box decoration as both of them serve the same purpose.

    The below shows the code snippet without decoration color but with container color.

    Container(
          decoration:
              BoxDecoration(shape: BoxShape.rectangle),
          child: const Icon(Icons.person),
          color: Colors.grey,
        );
    

    The below shows the code snippet with decoration color but not container color.

    Container(
          decoration:
              BoxDecoration(shape: BoxShape.rectangle, color: Colors.lightGreen),
          child: const Icon(Icons.person),
        );
    

    In your case, use the later as it seems that is what you were trying to achieve, from your question.

    Login or Signup to reply.
  2. A Container widget can have a decoration property and a color property, but you should only specify one of them. If you specify both properties, the color property will be ignored and overridden by the BoxDecoration specified in the decoration property.

    To fix the error, you can remove the color property and specify the color in the BoxDecoration instead. Here’s the corrected code:

    Container(decoration:BoxDecoration(shape: BoxShape.rectangle, color: Colors.lightGreen ), child: const Icon(Icons.person),),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search