首页 > 解决方案 > TypeError:无法读取 null 的属性“Symbol(dartx._get)”(在 Flutter 中添加 Firestore 侦听器时)

问题描述

我无法向我的 firestore 文档添加监听器。我收到上述错误,我不明白是什么原因造成的。我的代码如下:

 StreamSubscription<DocumentSnapshot> listener;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_){
      _initListener();
    });
  }

  _initListener() async {
    listener = FirebaseFirestore.instance
        .collection('Ref to my collection path')
        .doc('status')
        .snapshots()
        .listen((DocumentSnapshot documentSnapshot) {
            Map<String, dynamic> firestoreInfo = documentSnapshot.data();
            setState(() {
              paid = firestoreInfo['status'];
            });
            if(paid) Navigator.pop(context);
          });
      listener.onError((handleError){
      if(DEBUG) print('Cannot attach listener: Error: $handleError');
    });
  }

  @override
  void dispose() {
    listener.cancel();
    super.dispose();
  }

编辑:我的构建方法如下:我正在使用网络视图插件来显示支付页面。我的目的是读取 firebase 上的数据并查看它何时更新,然后转到付款成功或付款失败页面。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Colors.white, //change your color here
        ),
        elevation: 0.0,
        title: Text("PAYMENT", style: Header),
        backgroundColor: Provider.of<CP>(context, listen: false).primary,
      ),
      backgroundColor: Color(0xfff6f7f8),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(13.0),
            child: Text('Please enter your payment details'),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.all(20),
              child: EasyWebView(
                onLoaded: (){},
                src: 'PATH FOR PAYMENT URL',
                isHtml: false, // Use Html syntax
                isMarkdown: false, // Use markdown syntax
                convertToWidgets: false, // Try to convert to flutter widgets
                width: MediaQuery.of(context).size.width,
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(13.0),
            child: Text('Your payment info is encrypted and protected by STRIPE', style: TextStyle(fontSize: 12, fontStyle: FontStyle.italic),),
          ),
        ],
      ),
    );
  }

标签: firebasefluttergoogle-cloud-firestore

解决方案


我认为你的问题在这里:

.listen((DocumentSnapshot documentSnapshot) {
            Map<String, dynamic> firestoreInfo = documentSnapshot.data();
            setState(() {
              paid = firestoreInfo['status'];
            });
            if(paid) Navigator.pop(context); // issue
          });

你注意到paid 没有setState在它里面初始化,所以你的 if 语句没有对paid. 将其更改为:

.listen((DocumentSnapshot documentSnapshot) {
            Map<String, dynamic> firestoreInfo = documentSnapshot.data();
            setState(() {
              paid = firestoreInfo['status'];
            });
            if(firestoreInfo['status']) Navigator.pop(context);
        });

如果您有任何问题或这没有解决您的问题,请在下方评论


推荐阅读