Search code examples
flutterprovider

The argument type 'Null' can't be assigned to the parameter type 'Products Function(BuildContext)


I want to pass data of one provider to another provider . so I have used ChangeNotifierProxyProvider . In here:

 ChangeNotifierProxyProvider<Auth, Products>(
            update: (ctx, _auth, previousProducts) => Products(
                _auth.token as String,
                previousProducts == null ? [] : previousProducts.items),
                create: null,                      //Here

          ),

on create parameter there is error called 'Null' can't be assigned to the parameter type 'Products Function(BuildContext). I just confuse about create and update parameter. can ayone explain about it. error----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- enter image description here

import 'package:flutter/material.dart';
import 'package:shoppingapp/Providers/Products.dart';
import 'package:shoppingapp/Providers/auth.dart';
import 'package:shoppingapp/Providers/order.dart';
import 'package:shoppingapp/Screens/auth_screen.dart';
import 'package:shoppingapp/Screens/Cart_Screen.dart';
import 'package:shoppingapp/Screens/Product_overview_screen.dart';
import 'package:shoppingapp/Screens/edit_product_screen.dart';
import 'package:shoppingapp/Screens/order_screen.dart';
import 'package:shoppingapp/Screens/product_detail_screen.dart';
import 'package:provider/provider.dart';
import 'package:shoppingapp/Screens/user_products_screen.dart';

import 'Providers/cart.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: Auth()),
          ChangeNotifierProxyProvider<Auth, Products>(
            update: (ctx, _auth, previousProducts) => Products(
                _auth.token as String,
                previousProducts == null ? [] : previousProducts.items),
                create: null,                      //error
               
          ),
          ChangeNotifierProvider(
            create: (ctx) => Cart(),
          ),
          ChangeNotifierProvider(
            create: (ctx) => Order(),
          )
        ],
        child: Consumer<Auth>(
          builder: (ctx, auth, _) => MaterialApp(
            theme: ThemeData(
              primarySwatch: Colors.blueGrey,
              accentColor: Colors.red,
              fontFamily: 'Lato',
            ),
            home: auth.isAuth ? ProductOverviewScreen() : AuthScreen(),
            routes: {
              ProductDetailScreen.routeName: (ctx) => ProductDetailScreen(),
              CartScreen.routeName: (ctx) => CartScreen(),
              OrderScreen.routeName: (ctx) => OrderScreen(),
              UserProductScreen.routeName: (ctx) => UserProductScreen(),
              EditProductScreen.routeName: (ctx) => EditProductScreen(),
            },
            debugShowCheckedModeBanner: false,
          ),
        ));
  }
}

Solution

  • If you are following Maximillian's course then this simple trick would work perfectly

    ChangeNotifierProxyProvider<Auth, Products>(
                update: (ctx, _auth, previousProducts) => Products(
                    _auth.token as String,
                    previousProducts == null ? [] : previousProducts.items),
                    create: Products(
                    '',
                    [],
                 ),
    
              ),