预期的 ';'在这之后。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType)

Expected ';' after this. , stthrow ProviderNotFoundException(T, context.widget.runtimeType)

问。我在 MAIN 文件中以如下所示的方式实现了 MultiProvider。虽然 运行 我抛出以下错误。有人可以帮我吗? 谢谢。

#错误

../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358 :7: 错误:需要一个标识符,但得到了 ','。 尝试在“,”之前插入一个标识符。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType); ^ ../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:357:49:错误:预期的 ';'在这之后。 if (inheritedElement == null && null is!T) { ^ ../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:7:错误:意外的标记 ','。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType); ^ ../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:9:错误: 'stthrow' 不是一种类型。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType); ^^^^^^^ ../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:17:错误:预期的 ';'在这之后。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType); ^^^^^^^^^^^^^^^^^^^^^^^^^^ ../../../../../../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/provider-6.0.1/lib/src/provider.dart:358:44:错误:在此之前应为“)”。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType); ^

失败:构建失败,出现异常。

Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

22 秒内构建失败 运行宁Gradle任务'assembleDebug'... 24.4s 异常:Gradle 任务 assembleDebug 失败,退出代码为 1


#我的主文件

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:e_shop/Config/config.dart';
import 'package:e_shop/Counters/changeAddress.dart';
import 'package:e_shop/Counters/itemQuantity.dart';
import 'package:e_shop/Counters/totalAmount.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'Config/config.dart';
import 'Counters/cartItemCounter.dart';
import 'splash/splashscreen.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  EcommerceApp.auth = FirebaseAuth.instance;
  EcommerceApp.sharedPreferences = await SharedPreferences.getInstance();
  EcommerceApp.firestore = FirebaseFirestore.instance;

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => CartItemCounter()),
        ChangeNotifierProvider(create: (context) => ItemQuantityCounter()),
        ChangeNotifierProvider(create: (context) => AddressChangeCounter()),
        ChangeNotifierProvider(create: (context) => TotalAmountCounter()),
        //OR -------
        // ChangeNotifierProvider.value(value: CartItemCounter()),
        // ChangeNotifierProvider.value(value: ItemQuantityCounter()),
        // ChangeNotifierProvider.value(value: AddressChangeCounter()),
        // ChangeNotifierProvider.value(value: TotalAmountCounter()),
      ],
      child: MaterialApp(
          title: 'e-Shop',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primaryColor: Colors.green,
          ),
          home: SplashScreen()),
    );
  }
}

#My Counter dart 文件(4 个文件与以下文件几乎相似)

import 'package:flutter/foundation.dart';

class CartItemCounter with ChangeNotifier {
  int _counter = EcommerceApp.sharedPreferences!
          .getStringList(EcommerceApp.userCartList)!
          .length -
      1;

  int get count => _counter;

  Future<void> displayResult() async {
    int _counter = EcommerceApp.sharedPreferences!
            .getStringList(EcommerceApp.userCartList)!
            .length -
        1;

    await Future.delayed(Duration(milliseconds: 100), () {
      notifyListeners();
    });
  }
}

当我构建提供程序时 class,我导入这个

import 'package:flutter/cupertino.dart';

而不是这个

import 'package:flutter/foundation.dart';

出于好奇,您在主文件中导入了哪个包?

--编辑: 也许您可以尝试在您的 changeNotifier 中使用构建器而不是子级,如下所示:

builder: (context, child) => MaterialApp(...)

MultiProvider/Provider 必须在 Widget 树的顶部提供。即,在 runApp 小部件内,它摆脱了 ProviderNotFoundException。,因为现在提供程序服务在整个应用程序中可用。

  Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  EcommerceApp.auth = FirebaseAuth.instance;
      
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (context) => Counter()),
      ChangeNotifierProvider(create: (context) => CartItemCounter()),
      ChangeNotifierProvider(create: (context) => AddressChangeCounter()),
      ChangeNotifierProvider(create: (context) => ItemQuantityCounter()),
      ChangeNotifierProvider(create: (context) => AddressChangeCounter()),
      ChangeNotifierProvider(create: (context) => TotalAmountCounter()),
    ],
    child: MyApp(),
  ));
}