【发布时间】:2021-04-09 19:16:05
【问题描述】:
我无法向我的 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),),
),
],
),
);
}
【问题讨论】:
-
发布您的
build方法或整个页面 -
@dm_tr 添加了构建方法
标签: firebase flutter google-cloud-firestore