error: The values in a const list literal must be constants. Flutter

Issue

can any one tell me why is there an error. im a beginner in flutter. can any one help.

this is the code, the error is on the onpressed fucntion.

Expanded(
                child: TextButton(
                  onPressed: () {},
                  child: Padding(
                    padding: EdgeInsets.all(16.0),
                    child: Image(
                      image: AssetImage('images/dice1.png'),
                    )
                  ),
    
                ),
    
              ),

error:

error: Invalid constant value. (invalid_constant at line 3)
error: The values in a const list literal must be constants. (non_constant_list_element at line 3)

Solution

Don’t use the const keyword if you are not using fixed values. Flutter uses the const keyword as an indicator for a widget to never rebuild as it will get evaluated at compile time and only once.

Answered By – Jignesh Patel

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.