Home > Software engineering >  Error: Cannot invoke a non-'const' constructor where a const expression is expected. Try u
Error: Cannot invoke a non-'const' constructor where a const expression is expected. Try u

Time:11-19

when I try to wrap the second padding in a row within a column, this error keeps coming. and there is no const in the parent widget. any help, please?

Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
                           Container(
                           ^^^^^^^^^
    
        child: Column(
                             children: [
                               Image.asset("images/2.jpg", height: 140, width: 195,),
                               Row(
                                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                 children: const <Widget>[
                                   Padding(
                                     padding: EdgeInsets.all(8.0),
                                     child: Text("ሓደ መኣዲ"),
                                   ),
                                   Container(
                                     child: Padding(
                                       padding: EdgeInsets.all(8.0),
                                       child: Icon(Icons.favorite_border, color: Colors.red,),
                                     ),
                                   ),

CodePudding user response:

remove the const from the Row and instead add it to the padding widget and Container widget like this:

 Row(
       mainAxisAlignment: MainAxisAlignment.spaceBetween,
       children:  <Widget>[
         const Padding(
          padding: EdgeInsets.all(8.0),
          child: Text("ሓደ መኣዲ"),
           ),
           Container(
            child: const Padding(
             padding: EdgeInsets.all(8.0),
               child: Icon(Icons.favorite_border, color: Colors.red,),
             ),
          ),
         ],
),

CodePudding user response:

just remove const before the children of the Row

                           Column(
                                 children: [
                                   Image.asset("images/2.jpg", height: 140, width: 195,),
                                   Row(
                                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                     children: <Widget>[
                                       Padding(
                                         padding: EdgeInsets.all(8.0),
                                         child: Text("ሓደ መኣዲ"),
                                       ),
                                       Container(
                                         child: Padding(
                                           padding: EdgeInsets.all(8.0),
                                           child: Icon(Icons.favorite_border, color: Colors.red,),
                                         ),
                                       ),
  • Related