Santo Shakil
Santo Shakil

Reputation: 1052

Can't create ListView.builder with a list

I tried to build a ListView.builder with a List.

It Shows me this Error: The argument type 'CartItem' can't be assigned to the parameter type 'String'

Here is my code:

class CartItem {
  String name;
  String quantity;
  String price;

  CartItem({
    this.name,
    this.quantity,
    this.price,
  });
}

List<CartItem> cartItem = [];

var _quantity = TextEditingController();

Widget cartList() {
  if (cartItem.length != 0) {
    return ListView.builder(
      itemCount: cartItem.length,
      itemBuilder: (context, index) {
        return Text(cartItem[index]);
      },
    );
  }
  return Text('Nothing in Cart');
}

And here is the implementation of my widget method:

RaisedButton(
                    elevation: 1,
                    color: Colors.blueGrey,
                    onPressed: () {
                      showDialog(
                        context: context,
                        builder: (BuildContext contex) {
                          return AlertDialog(
                            content: Column(
                              children: [
                                Text('Your Order List'),
                                Expanded(
                                  child: cartList(),
                                )
                              ],
                            ),
                          );
                        },
                      );
                    },
                    child: Text(
                      'Conferm Order',
                      textAlign: TextAlign.end,
                    ),
                  ),

enter image description here

Upvotes: 0

Views: 388

Answers (1)

AskNilesh
AskNilesh

Reputation: 69754

The Text widget required String as an argument while cartItem[index] returned the instance of CartItem that's why you are getting an error

You should use

return Text(cartItem[index].name);

instead of this

return Text(cartItem[index]);

SAMPLE CODE

Widget cartList() {
  if (cartItem.length != 0) {
    return ListView.builder(
      itemCount: cartItem.length,
      itemBuilder: (context, index) {
        return Text(cartItem[index].name);
      },
    );
  }
  return Text('Nothing in Cart');
}

Upvotes: 2

Related Questions