0

I'm trying to send a product's index to a method but when i tap the button that calls the method throws me an the error the method wass called on 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'), 
              )
            ],
          ),
        )
      ),
    );  
  }
}
  • 2
    What's your question? `model` is `null`. – Christopher Moore Aug 28 '20 at 23:06
  • Hi, interesting, perhaps print the product list before the call to verify it has the expected values? – IronMan Aug 28 '20 at 23:06
  • 1
    @IronMan That would do no good. We already know `model` is null. – Christopher Moore Aug 28 '20 at 23:09
  • The problem is in the error message. "The method 'addProduct' was called on null" means you called `addProduct` on a null object. By process of elimination, that means that `model` is null. – Abion47 Aug 28 '20 at 23:21
  • Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Nov 23 '20 at 14:34

0 Answers0