参数类型 'Null' 不能分配给参数类型 'Products Function(BuildContext)

时间:2021-07-13 20:17:30

标签: flutter provider

我想将一个提供者的数据传递给另一个提供者。所以我使用了 ChangeNotifierProxyProvider 。 在这里:

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

          ),

在创建参数时出现名为“Null”的错误,无法分配给参数类型“产品函数(BuildContext)”。我只是对创建和更新参数感到困惑。谁能解释一下。 错误 - - - - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------ 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,
          ),
        ));
  }
}

0 个答案:

没有答案