Flutter 使用StreamBuilder在提供程序中存储数据,但出现错误

Flutter 使用StreamBuilder在提供程序中存储数据,但出现错误,flutter,google-cloud-firestore,streaming,flutter-provider,Flutter,Google Cloud Firestore,Streaming,Flutter Provider,我试图使用StreamBuilder将数据从firestore获取到提供程序中,但出现错误。 我所做的 我把听错了 它在演示中,所以仅尝试从快照中获取数据并将其存储在changeNotifier中,我不这样做 现在在任何地方使用加载的数据 我试图在这里搜索,但没有得到任何解决方案,请帮助 谢谢 我的代码是: @override Widget build(BuildContext context) { return ChangeNotifierProvider( cre

我试图使用StreamBuilder将数据从firestore获取到提供程序中,但出现错误。 我所做的

  • 我把听错了
  • 它在演示中,所以仅尝试从快照中获取数据并将其存储在changeNotifier中,我不这样做 现在在任何地方使用加载的数据 我试图在这里搜索,但没有得到任何解决方案,请帮助 谢谢 我的代码是:

      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider(
          create: (context) => CartItemFromFirebase(),
          child: WillPopScope(
            onWillPop: onWillPop,
            child: StreamBuilder<User>(
              stream: FirebaseAuth.instance.authStateChanges(),
              builder: (context, snapshot) {
                if (snapshot.hasData && snapshot.data != null) {
                  userr = snapshot.data;
                  print('recd dara : ${snapshot.data}');
    
                  UserHelper.saveUser(snapshot.data);
                  return Scaffold(
                    body: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
                        stream: FirebaseFirestore.instance
                            .collection('cart')
                            .doc(userr.uid)
                            .snapshots(),
                        builder: (context, snapshot) {
                          if (snapshot.connectionState == ConnectionState.waiting) {
                            return CircularProgressIndicator();
                          }
                          if (snapshot.hasError) {
                            return Text('Something went wrong in your Cart');
                          }
                          Map<String, dynamic> data =
                              snapshot.data.data()['cart'] as Map;
                          print('d isssss ata  $data');
                          Provider.of<CartItemFromFirebase>(context, listen: false)
                              .addToCartItem(data);
    
                          return MainHomeScreen();
                        }),
                  );
                }
                return LoginScreen();
              },
            ),
          ),
        );
      }
    }```
    
    My ChangeNotifier is
    
    class Prop {
      String wt;
      int quant;
      Prop({this.quant, this.wt});
    }
    
    class CartItemFromFirebase with ChangeNotifier {
      Map<String, Prop> _cartItem = {};
      Map<String, dynamic> get cartItem {
        return {..._cartItem};
      }
    
      addToCartItem(Map<String, dynamic> data) {
        data.forEach((key, value) {
          print('jey $key  Value $value');
        });
        data.forEach((keyA, valueA) {
          if (_cartItem.containsKey(keyA)) {
            _cartItem.update(
                keyA, (value) => Prop(quant: int.parse(valueA[1]), wt: valueA[0]));
          } else {
            _cartItem.putIfAbsent(
                keyA, () => Prop(quant: valueA[1], wt: valueA[0].toString()));
          }
        });
    
        notifyListeners();
      }
    }
    
    @覆盖
    小部件构建(构建上下文){
    返回ChangeNotifierProvider(
    create:(context)=>CartItemFromFirebase(),
    孩子:Willposcope(
    onWillPop:onWillPop,
    孩子:StreamBuilder(
    流:FirebaseAuth.instance.authStateChanges(),
    生成器:(上下文,快照){
    if(snapshot.hasData&&snapshot.data!=null){
    userr=snapshot.data;
    打印('recd dara:${snapshot.data}');
    UserHelper.saveUser(snapshot.data);
    返回脚手架(
    正文:StreamBuilder(
    流:FirebaseFirestore.instance
    .collection('购物车')
    .doc(userr.uid)
    .snapshots(),
    生成器:(上下文,快照){
    if(snapshot.connectionState==connectionState.waiting){
    返回循环ProgressIndicator();
    }
    if(snapshot.hasError){
    返回文本(“您的购物车出现问题”);
    }
    地图数据=
    snapshot.data.data()['cart']作为映射;
    打印('d ISSSS ata$data');
    Provider.of(上下文,侦听:false)
    .AddToCaritem(数据);
    返回主屏幕();
    }),
    );
    }
    返回LoginScreen();
    },
    ),
    ),
    );
    }
    }```
    我的变更通知是
    类道具{
    字符串wt;
    整数;
    Prop({this.quant,this.wt});
    }
    使用ChangeNotifier为CartItemFromFirebase初始化{
    映射_cartItem={};
    映射获取cartItem{
    返回{…\u cartItem};
    }
    AddToCarItem(地图数据){
    data.forEach((键,值){
    打印(“jey$键值$键值”);
    });
    data.forEach((keyA,valueA){
    如果(_cartItem.containsKey(keyA)){
    _cartItem.update(
    keyA,(value)=>Prop(quant:int.parse(valueA[1]),wt:valueA[0]);
    }否则{
    _cartItem.putIfAbsent(
    keyA,()=>Prop(quant:valueA[1],wt:valueA[0].toString());
    }
    });
    notifyListeners();
    }
    }
    
    错误是

    I/flutter ( 5035): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
    I/flutter ( 5035): The following assertion was thrown while dispatching notifications for CartItemFromFirebase:
    I/flutter ( 5035): setState() or markNeedsBuild() called during build.
    I/flutter ( 5035): This _InheritedProviderScope<CartItemFromFirebase> widget cannot be marked as needing to build
    I/flutter ( 5035): because the framework is already in the process of building widgets.  A widget can be marked as
    I/flutter ( 5035): needing to be built during the build phase only if one of its ancestors is currently building. This
    I/flutter ( 5035): exception is allowed because the framework builds parent widgets before children, which means a
    I/flutter ( 5035): dirty descendant will always be built. Otherwise, the framework might not visit this widget during
    I/flutter ( 5035): this build phase.
    I/flutter ( 5035): The widget on which setState() or markNeedsBuild() was called was:
    I/flutter ( 5035):   _InheritedProviderScope<CartItemFromFirebase>
    I/flutter ( 5035): The widget which was currently being built when the offending call was made was:
    I/flutter ( 5035):   StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>
    I/flutter ( 5035): When the exception was thrown, this was the stack:
    
    I/颤振(5035):══╡ 基金会例外╞════════════════════════════════════════════════════════
    I/flatter(5035):在为CartItemFromFirebase发送通知时引发了以下断言:
    I/flatter(5035):在生成期间调用setState()或markNeedsBuild()。
    I/flatter(5035):此_InheritedProviderScope小部件不能标记为需要生成
    I/flatter(5035):因为框架已经在构建小部件。小部件可以标记为
    I/flatter(5035):仅当其祖先之一当前正在构建时,才需要在构建阶段构建。这
    I/flatter(5035):允许出现异常,因为框架在子窗口之前构建父窗口小部件,这意味着
    我/弗利特(5035):肮脏的后代将永远被建造。否则,框架可能不会在运行期间访问此小部件
    I/颤振(5035):此构建阶段。
    I/flatter(5035):调用setState()或markNeedsBuild()的小部件是:
    I/颤振(5035):\u继承ProviderScope
    I/flatter(5035):当发出违规呼叫时,当前正在构建的小部件是:
    I/颤振(5035):StreamBuilder
    I/flatter(5035):当抛出异常时,这是堆栈: