【问题标题】:How to pull values from firebase instead of hard code如何从firebase而不是硬代码中提取值
【发布时间】:2019-12-29 07:00:06
【问题描述】:

我正在用 Flutter 构建个人资料页面,我想知道用从 Firestore 中提取的值简单地替换硬编码值的最佳方法是什么。有人对如何最好地做到这一点有任何建议吗?

没有发现教程有用。

我正在尝试用从 firestore 中提取的值替换以下数值。

new Row(
                  children: <Widget>[
                    rowCell(343, 'POSTS'),
                    rowCell(673826, 'FOLLOWERS'),
                    rowCell(275, 'FOLLOWING'),
                  ],),

【问题讨论】:

    标签: android firebase android-studio flutter android-emulator


    【解决方案1】:

    理想情况下,您希望将用户 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);
    }
    

    【讨论】:

    • 谢谢,萨尔玛!我已经这样做了,但是 rowCell 函数出现错误。我现在真的只需要从firestore中提取'name'值。您对可能出现的问题有任何想法吗?
    猜你喜欢
    • 2020-03-30
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多