Я работаю с приложением корзины покупок и использую flutter_redux для обновления моей корзины. Все работает хорошо, но я получаю дубликаты в моем списке корзин. Как я могу удалить эти дубликаты и добавить количества. Я хочу получить список с деталями и количеством. Пожалуйста, помогите мне с этим фрагментом кода, где я могу добавить логику и отфильтровать список с удаленными дублирующимися элементами.
Вот мой код:
cartItemReducer.dart
List<CartItem> cartItemReducer(List<CartItem> items, dynamic action)
{
if(action is AddItemAction)
{
return addItem(items, action);
}
else if (action is DeleteItemAction)
{
return deleteItem(items, action);
}
return items;
}
List<CartItem> addItem(List<CartItem> items, AddItemAction action)
{
return new List.from(items)..add(action.item);
}
List<CartItem> deleteItem(List<CartItem> items, DeleteItemAction action)
{
return new List.from(items)..remove(action.item);
}
добавление товаров в мою корзину из этого кода:
Widget futureWidget()
{
return new FutureBuilder<List<ModelProductDetail>>(
future: getDetailProductFuture(),
builder: (BuildContext context, AsyncSnapshot snapshot){
if(snapshot.hasData)
{
Widget addRemoveButtonRedux = new StoreConnector<
List<CartItem>, OnItemAddedCallback>(
converter: (store)
=>(itemName) => store.dispatch(
AddItemAction(CartItem(itemName.product, itemName.quantity
))),
builder: (context, callback) =>
new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.minus_circled,
color: Colors.cyan,
semanticLabel: 'Subtract',
),
onPressed: quantity == 0 ? null : () => decrement(),
),
new Container(
decoration: new BoxDecoration(
border: new Border.all(
color: Colors.grey[700],
width: 0.5
)
),
child: new SizedBox(
width: 40.0,
height: 30.0,
child: new Center(
child: new Text('$quantity',
style: Theme
.of(context)
.textTheme
.subhead,
textAlign: TextAlign.center,),
),
),
),
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.plus_circled,
color: Colors.cyan,
semanticLabel: 'ADD',
),
onPressed: () => increment(
callback
),
),
],
),
) ,
);
//getDetailProduct();
Widget addRemoveButton = new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.minus_circled,
color: Colors.cyan,
semanticLabel: 'Subtract',
),
onPressed: quantity == 0 ? null : () => decrement(),
),
new Container(
decoration: new BoxDecoration(
border: new Border.all(
color: Colors.grey[700],
width: 0.5
)
),
child: new SizedBox(
width: 40.0,
height: 30.0,
child: new Center(
child: new Text('$quantity',
style: Theme
.of(context)
.textTheme
.subhead,
textAlign: TextAlign.center,),
),
),
),
new CupertinoButton(
padding: EdgeInsets.zero,
child: new Icon(
CupertinoIcons.plus_circled,
color: Colors.cyan,
semanticLabel: 'ADD',
),
onPressed: () {},
),
],
),
);
Widget price = new Row(
children: <Widget>[
new DecoratedBox(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.circular(30.0)
),
child: new ClipRRect(
borderRadius: new BorderRadius.circular(50.0),
child: new MaterialButton(onPressed: null,
minWidth: 20.0,
color: Colors.grey[900],
child: new Text("€${modelList[0].price}", style: new TextStyle(
color: Colors.white,
fontSize: 17.0,
),),
),
),
),
],
);
Widget titleSection = new Container(
padding: const EdgeInsets.all(20.0),
child: new Row(
children: [
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
new Container(
padding: const EdgeInsets.only(bottom: 8.0),
child: new Text(modelList[0].name, style: new TextStyle(
fontSize: 20.9,
fontWeight: FontWeight.bold
),),
),
//new Text("Corn Pizza"),
price
]
)),
new Expanded(child: new Column(
children: <Widget>[
// price,
addRemoveButtonRedux
],
))
],
),
);
Widget description = new Container(
//padding: const EdgeInsets.all(20.0),
child: new Column(
children: [
new Card(
child: new Container(
width: 400.0,
height: 200.0,
padding: const EdgeInsets.all(5.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text("Description: ", style: new TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold
),),
new Padding(padding: const EdgeInsets.only
(bottom: 20.0)),
isDesription ? new Text("Description not available") : new Text(
modelList[0].description),
],
),
)
)
]
));
modelList = snapshot.data;
//print("Snapshot: "+modelList[0].description);
//print("Snapshot: "+modelList[0].id.toString());
//print("Snapshot: "+modelList[0].price.toString());
List<ImageProvider> networkimages = <ImageProvider>[];
for(int i=0 ; i< modelList[0].listThumbnails.length; i++ )
{
networkimages.add(new NetworkImage(
CommonMethods.image_url+modelList[0].listThumbnails[i]
)
);
}
return new Container(
child: new ListView(
children: [
new ImageCarousel(
networkimages,
interval: new Duration(seconds: 5),
showCloseButtonOnZoom: true,
),
titleSection,
description,
]
)
);
}
else
{
// print("ERROR");
return new Center(
child: new Text("Loading Data....",
style: new TextStyle(fontSize: 20.0),),
);
}
});
}
вот методы увеличения и уменьшения.
decrement() {
setState(() {
quantity--;
});
}
increment(callback) {
setState(() {
quantity++;
Product product = new Product(modelList[0].name,
modelList[0].price,
modelList[0].id,
modelList[0].listThumbnails[0]);
CartItem cartItem = new CartItem(
product,
quantity
);
callback(cartItem);
});
}
Метод обратного вызова:
typedef OnItemAddedCallback = Function(CartItem itemName);