android - 如何从firebase而不是硬代码中提取值
问题描述
我正在颤振中构建一个个人资料页面,我想知道用从 Firestore 中提取的值简单地替换硬编码值的最佳方法是什么。有没有人对如何最好地做到这一点有任何建议?
没有发现教程有用。
我正在尝试用从 Firestore 中提取的值替换以下数值。
new Row(
children: <Widget>[
rowCell(343, 'POSTS'),
rowCell(673826, 'FOLLOWERS'),
rowCell(275, 'FOLLOWING'),
],),
解决方案
理想情况下,您希望将用户 ID 传递给返回配置文件的小部件。例如
class ProfileWidget extends StatelessWidget {
final String userId;
ProfileWidget (this.userId);
@override
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: Firestore.instance
.collection('users')
.document(userId)
.snapshots(),
builder: (context, snapshot) {
User user = User.fromSnapshot(snapshot.data);
return Row(
children: <Widget>[
rowCell(user.posts, 'POSTS'),
rowCell(user.followers, 'FOLLOWERS'),
rowCell(user.following, 'FOLLOWING'),
],),
},
);},}
现在用户类:
class User{
final int following;
final int posts;
final int followers;
final DocumentReference reference;
User.fromMap(Map<String, dynamic> map, {this.reference})
: following = map['following'],
posts = map['posts'],
followers= map['followers'];
User.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);
}
推荐阅读
- wordpress - 将菜单的第一个字母和最后一个字母与左右容器边框对齐
- excel - 遍历给定文件夹中的文件,检查 CLOSED 文件中的单元格内容以识别模板并将所述文件添加到数组
- android - 从android的存储中获取位图时图像质量丢失
- spring-boot - Keycloak 和 Spring Boot
- javascript - 如何使用 vanilla Javascript 将文件的内容放入数组中?
- c++ - 如何在 C++ 中解析 8 位日期?
- reactjs - 有没有办法在 ReactJS 中向动态表中添加行?
- installation - 在 MAC 上安装 CPLEX Studio 20 时出现问题
- arrays - 从文本文件中的一些字符串创建数组 - Ruby
- xforms - XFDL 日期约束