引发了另一个异常:NoSuchMethodError:在null上调用了getter'id'。在Flutter中

问题描述 投票:0回答:1

[我正在使用flutter开发购物车应用程序。用户单击产品详细信息页面中的添加按钮后,应将产品添加到购物车。但是,当我单击添加按钮时,它显示出这样的错误Product id was called null

这是我的CartModel代码

import 'package:cart_application/model/product.dart';
    import 'package:flutter/material.dart';

    class CartBloc extends ChangeNotifier {
      Map<int, int> _cart = {};

      Map<int, int> get cart => _cart;

      void addToCart(ProductsListItem productsListItem) {
          _cart[productsListItem.id] ;

        notifyListeners();
      }

      void clear(ProductsListItem productsListItem) {
          _cart.remove(productsListItem.id);
          notifyListeners();
        }
      }

这是我的ProductListItemPage。

import 'package:flutter/material.dart';
import '../util/constants.dart';

class ProductsListItem extends StatelessWidget {
  final int id;
  final String name;
  final int currentPrice;
  final int originalPrice;
  final int discount;
  final String imageUrl;

  const ProductsListItem({Key key,
    this.id,
    this.name,
    this.currentPrice,
    this.originalPrice,
    this.discount,
    this.imageUrl})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        _buildProductItemCard(context),
        _buildProductItemCard(context),
      ],
    );
  }

  _buildProductItemCard(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(Constants.ROUTE_PRODUCT_DETAIL);
      },
      child: Card(
        elevation: 4.0,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              child: Image.network(
                imageUrl,
              ),
              height: 250.0,
              width: MediaQuery
                  .of(context)
                  .size
                  .width / 2.2,
            ),
            SizedBox(
              height: 8.0,
            ),
            Padding(
              padding: const EdgeInsets.only(
                left: 8.0,
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text(
                    name,
                    style: TextStyle(fontSize: 16.0, color: Colors.grey),
                  ),
                  SizedBox(
                    height: 2.0,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text(
                        "\$$currentPrice",
                        style: TextStyle(fontSize: 16.0, color: Colors.black),
                      ),
                      SizedBox(
                        width: 8.0,
                      ),
                      Text(
                        "\$$originalPrice",
                        style: TextStyle(
                          fontSize: 12.0,
                          color: Colors.grey,
                          decoration: TextDecoration.lineThrough,
                        ),
                      ),
                      SizedBox(
                        width: 8.0,
                      ),
                      Text(
                        "$discount\% off",
                        style: TextStyle(fontSize: 12.0, color: Colors.grey),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 8.0,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

这是我在ProductListPage中调用的添加到购物车的方法。

 _buildBottomNavigationBar(BuildContext context) {
    ProductsListItem  item;
    var bloc = Provider.of<CartBloc>(context);
    int totalCount = 0;
    if (bloc.cart.length > 0) {
      totalCount = bloc.cart.values.reduce((a, b) => a + b);
    }
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 50.0,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          Flexible(
            fit: FlexFit.tight,
            flex: 1,
            child: RaisedButton(
              onPressed: () {},
              color: Colors.grey,
              child: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Icon(
                      Icons.list,
                      color: Colors.white,
                    ),
                    SizedBox(
                      width: 4.0,
                    ),
                    Text(
                      "SAVE",
                      style: TextStyle(color: Colors.white),
                    ),
                  ],
                ),
              ),
            ),
          ),
          Flexible(
            flex: 2,
            **child: RaisedButton(
              onPressed: (){bloc.addToCart(item);},**


              color: Colors.greenAccent,
              child: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Icon(
                      Icons.card_travel,
                      color: Colors.white,
                    ),
                    SizedBox(
                      width: 4.0,
                    ),
                    Text(
                      "ADD TO BAG",
                      style: TextStyle(color: Colors.white),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

这是我的CartPage

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../model/cart_bloc.dart';

class CartPage extends StatefulWidget {
  CartPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {

  @override
  Widget build(BuildContext context) {

    var bloc = Provider.of<CartBloc>(context,listen: false);
    var cart = bloc.cart;
    return Scaffold(
      appBar: AppBar(
        title: Text("Shopping Cart"),
        leading: IconButton(
          icon: Icon(
            Icons.chevron_left,
            size: 40.0,
            color: Colors.white,
          ),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),

      ),
      body:
        ListView.builder(
          itemCount: cart.length,
          itemBuilder: (context, index) {
            int giftIndex = cart.keys.toList()[index];
            int count = cart[giftIndex];
            return ListTile(
              leading: Container(
                height: 70,
                width: 70,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("${giftIndex + 1}.jpg"),
                    fit: BoxFit.fitWidth,
                  ),
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              title: Text('Item Count: $count'),
              trailing: RaisedButton(
                child: Text('Clear'),
                color: Theme.of(context).buttonColor,
                elevation: 1.0,
                splashColor: Colors.blueGrey,
                onPressed: () {

                },
              ),
            );
          },
        ),

    );
  }
}

而且我正在使用Android Studio作为IDE。

android flutter shopping-cart
1个回答
0
投票

这可能不是这里的主要问题,但可以确保您确实将商品添加到购物车中。在add列表中永远不会调用网络_cart

void addToCart(ProductsListItem productsListItem) {
        //this
        _cart.add(productsListItem.id);

        //instead of this
        _cart[productsListItem.id];

        notifyListeners();
      }

也许主要问题不是从那里来的,但至少可以帮助您找到它。

© www.soinside.com 2019 - 2024. All rights reserved.