Home > Back-end >  Error: The argument type 'String' can't be assigned to the parameter type 'List&
Error: The argument type 'String' can't be assigned to the parameter type 'List&

Time:08-08

I have a NameClass that contains a list of strings to hold each dish name and a list of price of each dish eaten

class NameClass extends StatelessWidget {
  List<String> foodName = [];
  List<double> foodPrice = [];
  NameClass({this.foodName, this.foodPrice});
  @override
  Widget build(BuildContext context) {}

}

And I enter the dish name and the price through a text field and keep assigning those values to the list

class FoodDetailsState extends State<FoodDetails> {
      
  List<NameClass> nameClassList = [];


ElevatedButton(
                              onPressed: () {
                                setState(
                                  () {
                                      nameClassList.add(NameClass(
                                      foodName: foodNameControllor.text,
                                      foodPrice: double.parse(foodPriceControllor.text),
                                    ));

                                  },
                                );
                              },
                              child: Icon(Icons.done, color: Colors.white),
                              style: ElevatedButton.styleFrom(
                                shape: CircleBorder(),
                                padding: EdgeInsets.all(20),
                              ),
                            )
}

But I get 2 errors that says

The argument type 'String' can't be assigned to the parameter type 'List'

The argument type 'double' can't be assigned to the parameter type 'List'.

How do I resolve this?

CodePudding user response:

On your NameClass, you are using List<String> and List<double>

class NameClass extends StatefulWidget {
  List<String> foodName = [];
  List<double> foodPrice = [];
  NameClass({ this.foodName, this.foodPrice});
  @override
  State<NameClass> createState() => _NameClassState();
}

but adding single string and value on

nameClassList.add(NameClass(
  foodName: foodNameControllor.text,,
  foodPrice: double.parse(foodPriceControllor.text),
));

You can do it like this pass list

nameClassList.add(NameClass(
  foodName: [foodNameControllor.text],
  foodPrice: [double.parse(foodPriceControllor.text)],
));

Or to accept single value

class NameClass extends StatefulWidget {
  final String  foodName ;
   final double foodPrice;
 const  NameClass({ required this.foodName,required  this.foodPrice});
  @override
  State<NameClass> createState() => _NameClassState();
}
  • Related