0

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

1 Answer 1

2

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your Answer is correct but I fatch another problem with my widget method. it show me an error: Another exception was thrown: Assertion failed: file:~/flutter/packages/flutter/lib/src/rendering/mouse_tra cking.dart:392:12
@SantoShakil try to restart your app
it doesn't work. I think this is another issue. but your answer is correct for my question of this post. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.