Я создал приложение для корзины покупок, используя Provide Package (State Management) для хранения выбранного элемента или продукта на странице корзины, но проблема в том, что я получаю сообщение об ошибке "type 'String' не является подтипом типа 'Item' ». Ниже приведен код и модели дротиков. Если я пытаюсь щелкнуть любой элемент на домашней странице, он сохраняет выбранный элемент на странице корзины, а если я пытаюсь сохранить элемент на второй странице (страница количеств), появляется сообщение об ошибке.
HomePage.dart
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();}
class _HomeState extends State<Home> {
List<Item> _product=[
Item(
title: "Cake",
image: "assets/1.png",
price: 20.00,
),
Item(
title: "Pasteries",
image: "assets/2.png",
price: 30.00,
),];
@override
Widget build(BuildContext context) {
return Consumer<Cart>(
builder: (context,cart,child){
return PlatformScaffold(
body: ListView.builder(
itemCount: _product.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.only(
top: 35.0, bottom: 15.0, left: 20.0, right: 20.0),
child: GestureDetector(
onTap: (){
//cart.add(_product[index]);//Here i try to select the item and it successfully stored in cart page
Navigator.of(context).push(MaterialPageRoute(builder: (context) => Quantities(
productname: _product[index].title,
productprice: _product[index].price,
productimage: _product[index].image,
)));},
child: Container(
child: new FittedBox(
child: Material(
color: Colors.white,
elevation: 15.0,
borderRadius: BorderRadius.circular(15.0),
shadowColor: Color(0x802196F3),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 250,
height: 200,
child: ClipRRect(
borderRadius: new BorderRadius.circular(15.0),
child: new Image.asset(
_product[index].image,
fit: BoxFit.cover,),),),
Padding(
padding: const EdgeInsets.only(top: 5.0,bottom: 5.0),
child: Text(_product[index].title,style: TextStyle(color: Colors.blueGrey[700],
fontWeight: FontWeight.bold,fontSize: 18.0),),
),],)),),),),);}));},);}}
Quantities.dart
class Quantities extends StatefulWidget {
var productprice;
String productimage;
final productname;
Quantities({this.productprice, this.productimage, this.productname});
@override
_QuantitiesState createState() => _QuantitiesState(productprice,productimage,productname);}
class _QuantitiesState extends State<Quantities> {
final productprice;
final productimage;
final productname;
_QuantitiesState(this.productprice, this.productimage, this.productname);
@override
Widget build(BuildContext context) {
return Consumer<Cart>(
builder: (context,cart,child){
return PlatformScaffold(
appBar: PlatformAppBar(
backgroundColor: Colors.lightBlue[900],
title: Text('Details'),),
body: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Container(
child: Column(
children: <Widget>[
Container(
height: 150.0,
child: GridTile(
child: Container(
color: Colors.white,
child: Image.asset(productimage),),),),
Text(productname,style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),
Text("Price: "+productprice.toString()+" SAR",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Center(
child: PlatformButton(
onPressed: () {
cart.add(productname);},// Here i am getting an error
child: Text('Add to Cart',style: TextStyle(color: Colors.white),),
androidFlat: (_) => MaterialFlatButtonData(
color: Colors.cyan),
ios: (_) => CupertinoButtonData(
color: Colors.cyan
)),),),],),),),],),);},);}}
CartPage.dart
class CartPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _CartPageState();}}
class _CartPageState extends State<CartPage> {
@override
Widget build(BuildContext context) {
return Consumer<Cart>(
builder: (context,cart,child){
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo,
title: Text("Cart"),),
body: cart.basketItems.length==0
?Text("no items"):ListView.builder(
itemCount: cart.basketItems.length,
itemBuilder: (context,index){
return Card(
child: ListTile(
title: Text(cart.basketItems[index].title),),);}),);},);}}
cartmodel.dart
class Item {
String title;
String image;
double price;
Item({this.title, this.price,this.image});}
Cart.dart
class Cart extends ChangeNotifier {
List<Item> _items = [];
double _totalPrice = 0.0;
void add(Item item) {
_items.add(item);
_totalPrice += item.price;
notifyListeners();}
void remove(Item item) {
_totalPrice -= item.price;
_items.remove(item);
notifyListeners();}
int get count {
return _items.length;}
double get totalPrice {
return _totalPrice;}
List<Item> get basketItems {
return _items;}}