Making unique ID list tile in Flutter

左心房为你撑大大i 提交于 2020-12-13 03:39:31

问题


I have this code that I can't get to work properly. I have a Food class and I have initialized the name, price, and unique ID strings on it. I made the unique ID to be Uuid().v4(), which would give each food item a random string.

FOOD CLASS

  class Food {
  String name;
  String price;
  String uniqueID = Uuid().v4();
  
  
  Food({this.name,
  this.price,
  this.uniqueID})}

On another page I have a Provider function that would add items in the cart, it is a list of string items (this may not be the best option). Here is the code for it:

class CartItemsModel extends ChangeNotifier {
  List<String> _cartItems = [];

  List<String> get cartItems => _cartItems;

  addCartItem(String item) {
    _cartItems.add(item);
    notifyListeners();
  }

Now, on another page, I am calling that food to be added to the cart, it is an icon with onPressed above function:

return ListTile(
      trailing: Container(
               padding:
                EdgeInsets.only(top: 15.0),
                 child: IconButton(
                   icon: Icon(Icons.add),
                                                
                     onPressed: () =>
                                                   
                        model.addCartItem(
                        "${food.name}, Calories: ${food.calories}        ${food.price} din\nVegan: ${food.isVegan},  ${Uuid().v4()}")),

Now, you see that I have Uuid on there (without my uniqueID from Food class, because for some reason it doesn't work). I have the Uuid, so that there isn't an error with multiple duplicate items if the button would be clicked twice, here's the error without it:

The issue is that this works and is functional, but I have this ugly ID from the Uuid displayed on the final 'cart' window. Basically I want this ID to be invisible. Here is how it looks:

And here is the code for the 'cart' screen:

  class _CartState extends State<Cart> {
  @override
  Widget build(BuildContext context) {
    return Consumer<CartItemsModel>(
      builder: (c, model, _) => Scaffold(
      body: SingleChildScrollView(
            child: Column(
              //on trailing i should have icon with clear function that will delete that item from the list
              children: model
                  .cartItems //maybe below can return ListView.separated like on the food list user, we'll see
                  
                  .map((e) =>   

So to keep long story short, my uniqueID isn't used on here because for some reason it doesn't make each list tile item unique with its key, so it doesn't display and give me error when clicked twice, that's why temporatily I am using the Uuid trick.

What I want is for this to work exactly like this, but without the ugly Uuid string to be seen. Is there simple I can do with it, maybe add something to the CartItemsModel code, a conditional, or something else?

If I change the code in onPressed to this:

onPressed: () {
     if (!model.cartItems
              .contains(food)) {
                model.addCartItem(Food);
                                    }
                                   }

I am getting error:

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

Which is understandable because it is a List in CartItemsModel. Any ideas how to fix this and be it just so items can be added to 'cart'?


回答1:


you keep it as the provider model. You keep it as a Food model, not a string. In onPressed you check if it is in it, if not you set it to provider If you want to write the data of cartItems in the Provider as a string, you can combine it. I realized you wanted it in the problem.

    class Food {
      String name;
      String price;
   
       Food({this.name,
       this.price})}

   class CartItemsModel extends ChangeNotifier {
     List<Food> _cartItems = [];
     List<Food> get cartItems => _cartItems;

     addCartItem(Food item) {
     _cartItems.add(item);
     notifyListeners();
     }

  onPressed: (){
     if(!cartItems.constains(food))model.addCartItem(Food);
    }



来源:https://stackoverflow.com/questions/65040880/making-unique-id-list-tile-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!