Method was called on null

I’m try to create a shopping cart but when I press the button add to cart that has to pass the idex of the product to the method it throws me the error the method add product wass called on null.

HomePage.dart

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Map _source = {ConnectivityResult.none: false};
  MyConnectivity _connectivity = MyConnectivity.instance;

  @override
  void initState() {
    super.initState();
    _connectivity.initialise();
    _connectivity.myStream.listen((source) {
      setState(() => _source = source);
    });
  }

  @override
  // ignore: missing_return
  Widget build(BuildContext context) {
    switch (_source.keys.toList()[0]) {
      case ConnectivityResult.none:
        return Scaffold(body: Center(child: TextData(data: 'Offline', fontSize: 35,),),);
        break;
      case ConnectivityResult.mobile:
      case ConnectivityResult.wifi:
        return DataList();
    }
  }

  @override
  void dispose() {
    _connectivity.disposeStream();
    super.dispose();
  }
}

class DataList extends StatelessWidget {
  const DataList({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(icon: Icon(Icons.shopping_cart, color: Colors.white), onPressed: () => Navigator.pushNamed(context, "ShoppingCart"))
        ],
      ),
      body: SafeArea(
        child: FutureBuilder<List<Product>>(
          future: fetchProducts(http.Client()),
          builder: (context, snapshot) {
            if (snapshot.hasError) print(snapshot.error);
            return snapshot.hasData
              ? ProductList(products: snapshot.data)
              : Center(child: CircularProgressIndicator());
          },
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            IconButton(icon: new Icon(MdiIcons.searchWeb), onPressed: (){}),
            IconButton(icon: new Icon(MdiIcons.home), onPressed: () => Navigator.pushNamed(context, "Homepage"),),
            IconButton(icon: new Icon(MdiIcons.map), onPressed: (){}),
            IconButton(icon: new Icon(MdiIcons.accountCircle), onPressed: () => Navigator.pushNamed(context, "UserAccount"),)
          ],
        ),
      ),
    );
  }
}

class ProductList extends StatelessWidget {
  final List<Product> products;
  ProductList({Key key, this.products}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
      ),
      itemCount: products.length,
      itemBuilder: (context, index) {
        return dataDisplay(products, index, context);
      },
    );
  }
}

Widget dataDisplay(products,index,context){
  return InkWell(
      child:Card(  
        child: Column(
          children: <Widget>[
            Expanded(
              child: Column(
                children: [
                  images(products, index),
                  Padding(
                    padding: const EdgeInsets.only(top:12.0),
                    child: Text(
                      products[index].nombre,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 19,
                        fontWeight: FontWeight.w500
                      ),
                    )
                  ),  
                ],
              )
            )
          ]  
        ),
    ),
    onTap: (){
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => ProductInfo(product:products[index], index: index,) 
        )
      );
    }
  );    
}


ProductInfo.dart

class ProductInfo extends StatelessWidget {
  final index;
  final Product product;
  final CartModel model;
  const ProductInfo({Key key, this.product,  this.model, this.index}) : 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(index),
                child: TextData(data: 'Agregar al carrito'),
                color: fromHex('#1389fd'), 
              )
            ],
          ),
        )
      ),
      bottomNavigationBar: BottomAppBar(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            IconButton(icon: new Icon(MdiIcons.searchWeb), onPressed: (){}),
            IconButton(icon: new Icon(MdiIcons.home), onPressed: () => Navigator.pushNamed(context, "Homepage"),),
            IconButton(icon: new Icon(MdiIcons.map), onPressed: (){}),
            IconButton(icon: new Icon(MdiIcons.accountCircle), onPressed: () => Navigator.pushNamed(context, "UserAccount"),)
          ],
        ),
      ),
    );  
  }
}

CartModel.dart

import 'package:eshop/models/products.dart';
import 'package:scoped_model/scoped_model.dart';

class CartModel extends Model {
  List<Product> cart = [];
  
  double totalCartValue = 0;

  int get total => cart.length;

  addProduct(product) {
    int index = cart.indexWhere((i) => i.idProducto == product.id);
    print(index);
    if (index != -1)
      updateProduct(product, product.qty + 1);
    else {
      cart.add(product);
      calculateTotal();
      notifyListeners();
    }
  }

  removeProduct(product) {
    int index = cart.indexWhere((i) => i.idProducto == product.id);
    cart[index].qty = 1;
    cart.removeWhere((item) => item.idProducto == product.id);
    calculateTotal();
    notifyListeners();
  }

  updateProduct(product, qty) {
    int index = cart.indexWhere((i) => i.idProducto == product.id);
    cart[index].qty = qty;
    if (cart[index].qty == 0)
      removeProduct(product);

    calculateTotal();
    notifyListeners();
  }

  clearCart() {
    cart.forEach((f) => f.qty = 1);
    cart = [];
    notifyListeners();
  }

  calculateTotal() {
    totalCartValue = 0;
    cart.forEach((f) {
      totalCartValue += f.precio * f.qty;
    });
  }
}

You are not passing the model to the ProductInfo Widget:

You should make any arguments that don’t have a default value @required. That way you avoid this mistakes :wink: