flutter - 如何在颤动的异步函数中访问字符串
问题描述
我在有状态小部件中有一个异步函数。在BuildContext下,我有这个功能,
getChildren() async {
final String numberOfChildren = await FirebaseFirestore.instance
.collection("children")
.where("parentUID", isEqualTo: uid)
.snapshots()
.length
.toString();
}
如何使用numberOfChildren
Text 小部件内部的变量。
Text(
'Children: $numberOfChildren',
style: TextStyle(
fontSize: 22,
color: Color(0xff74828E),
fontFamily: 'Rubik',
fontWeight: FontWeight.w900,
),
),
但它说numberOfChild
如何在文本小部件中使用它?
解决方案
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("children")
.where("parentUID", isEqualTo: uid)
.snapshots(), // async work
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting: return Text('Loading....');
default:
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else
return Text(
'Children: ${snapshot.data.docs.length}');
}
},
)
推荐阅读
- javascript - Javascript 对象不接受 JSON
- javascript - 在 Ruby on Rails 中操作数据库
- reactjs - 如何将 isSerializible 参数添加到 redux 存储设置
- java - 休眠问题:StaleObjectStateException with saveAndFlush()
- c++ - 是否可以使用自定义比较器使 C++ 标准排序稳定,如果我不想使用 stable_sort?
- windows - Windows 注册表 - UninstallString 与 QuietUninstallString
- python - os.stat(path) TypeError: stat: path should be string, bytes, os.PathLike or integer, not Entry
- html - 如何在 React 中使用第三方 SDK
- inspec - 在控制 rb 文件的开头检查标题?
- android - Ionic 3 -> TypeError: Printer_1.getPlugin(...).check 不是函数