微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 StreamBuilder 在 Provider 中存储数据但出错

如何解决使用 StreamBuilder 在 Provider 中存储数据但出错

我正在尝试使用 StreamBuilder 从 Firestore 获取数据到 Provider 中,但出现错误。 我做了什么

  1. 我把listen设为假;
  2. 它在演示中,所以只尝试从快照中获取数据并将其存储在 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(),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();
  }
}

错误

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:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。