Flutter 方法';添加产品&x27;被调用为空

Flutter 方法';添加产品&x27;被调用为空,flutter,dart,Flutter,Dart,我试图将一个产品的索引发送到一个方法,但当我点击调用该方法的按钮时,抛出一个错误,该方法被调用为null class ProductInfo extends StatelessWidget { final index; final Product product; final CartModel model; const ProductInfo({Key key, this.product, this.index, this.model}) : super(key: key);

我试图将一个产品的索引发送到一个方法,但当我点击调用该方法的按钮时,抛出一个错误,该方法被调用为null

class ProductInfo extends StatelessWidget {
  final index;
  final Product product;
  final CartModel model;
  const ProductInfo({Key key, this.product, this.index, this.model}) : super(key: key);
  

  @override
  Widget build(BuildContext context) {
    print(index);
    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(icon: Icon(Icons.shopping_cart, color: Colors.white), onPressed: () => Navigator.pushNamed(context, "ShoppingCart"))
        ],
      ),
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: <Widget>[
              images(product),
              productData(product),
              FlatButton(
                onPressed: () => model.addProduct(ProductList(products: [index],)),
                child: TextData(data: 'Agregar al carrito'),
                color: fromHex('#1389fd'), 
              )
            ],
          ),
        )
      ),
    );  
  }
}
class ProductInfo扩展了无状态小部件{
最终指标;
最终产品;
最终模型;
const ProductInfo({Key,this.product,this.index,this.model}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
打印(索引);
返回脚手架(
appBar:appBar(
行动:[
图标按钮(图标:图标(Icons.shopping_cart,颜色:Colors.white),按下:()=>Navigator.pushNamed(上下文,“ShoppingCart”))
],
),
正文:SingleChildScrollView(
子:容器(
子:列(
儿童:[
图像(产品),
产品数据(产品),
扁平按钮(
按下:()=>model.addProduct(产品列表(产品:[索引],),
child:TextData(数据:“Agregar al-carrito”),
颜色:fromHex(“#1389fd”),
)
],
),
)
),
);  
}
}

你的问题是什么
model
null
。您好,很有趣,也许在调用之前打印产品列表以验证其是否具有预期值?@IronMan,这没有什么好处。我们已经知道
model
为空。问题在于错误消息。“在null上调用了方法'addProduct'”表示您在null对象上调用了
addProduct
。通过消除过程,这意味着
模型
为空。这是否回答了您的问题?