从firestore中获取用户数据,并使用flutter在配置文件屏幕中显示用户数据

时间:2020-10-07 12:08:30

标签: firebase flutter dart google-cloud-firestore

这里的问题是,当我获取数据时,我想为当前用户获取数据,但实际上是为该集合中的所有用户获取数据。

我已经阅读并观看了许多视频,以寻求可能的解决方案,但我似乎找不到解决方法。请您的帮助。谢谢。

下面是该部分的摘录。


文件图像;

  TextEditingController loginNameController = TextEditingController();
  TextEditingController loginPhoneController = TextEditingController();
  TextEditingController loginAddressController = TextEditingController();

  clearForm() {
    setState(() {
      image = null;
      loginNameController.clear();
      loginPhoneController.clear();
      loginAddressController.clear();
    });
  }

  //=====> FOR INSTANCES OF FIREBASE <=====
  final auth = FirebaseAuth.instance;
  final db = FirebaseFirestore.instance;
  User user = FirebaseAuth.instance.currentUser;
   



 body: Padding(
                padding: EdgeInsets.only(left: 20, right: 20),
                child: StreamBuilder(
                  stream: db.collection("collection name").snapshots(),
                  builder: (BuildContext context, AsyncSnapshot snapshot){
                    if (!snapshot.hasData) {
                      return Center(
                        child: spinkit,
                      );
                    }
                    return ListView.builder (
                      itemCount: snapshot.data.docs.length,
                      itemBuilder: (BuildContext context, int index){
                        return Stack(
                          children: [
                            Column(
                              children: [
                                Stack(
                                  children: [
    
                                    // ===> RETRIEVING USER DETAILS AND SHOWING IT IN A ROW <===
                                    Container(
                                      padding : EdgeInsets.only(top: 10),
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          CircleAvatar(
                                            backgroundColor: Palette.mainColor,
                                            radius: 50,
                                            child: ClipOval(
                                              child: SizedBox(
                                                height: 150,
                                                width: 150,
                                                child: image == null ? Center(
                                                  // child: Image.asset("asset/images/placeholder.png", fit: BoxFit.cover,),
                                                  child: Image.network(snapshot.data.documents[index].get("image")),
                                                ):
                                                Image.file(image, fit: BoxFit.cover,),
                                              ),
                                            ),
                                          ),
                                          SizedBox(width: 16,),
                                          Expanded(
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: <Widget>[
                                                Padding(
                                                  padding: EdgeInsets.only(left: 0),
                                                  child: Text(snapshot.data.documents[index].get("Name"),
                                                    style: TextStyle(
                                                      letterSpacing: 2,
                                                      color: Colors.black,
                                                      fontSize: 18,
                                                      fontWeight: FontWeight.bold,
                                                    ),),
                                                ),
                                                SizedBox(height: 5,),
                                                Text(snapshot.data.documents[index].get("Address"),
                                                  style: TextStyle(
                                                    letterSpacing: 2,
                                                    color: Colors.black54,
                                                    fontSize: 16,
                                                  ),),
                                                SizedBox(height: 5,),
                                                Text(snapshot.data.documents[index].get("Number"),
                                                  style: TextStyle(
                                                    letterSpacing: 2,
                                                    color: Colors.black54,
                                                    fontSize: 16,
                                                  ),),
                                              ],
                                            ),
                                          ),
                                          Padding(
                                            padding: EdgeInsets.only(left: 0, bottom: 15),
                                            child: IconButton(
                                              icon:Icon(Icons.edit, color: Palette.mainColor, ),
                                              onPressed: () {  },
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
    
                                  ],
                                ),
                              ],
                            ),
                          ],
                        );
                      },
                    );
                  },
                ),
              )

A shot of the dB

集合名称是成员

1 个答案:

答案 0 :(得分:1)

尝试这样,如上所述,您的小部件流应该是这样的。

db.collection("Users").document(user.uid).snapshots();

Listview.builder 中的长度,也进行更改

snapshot.data.length;

最后,像这样获取数据的所有数据都应更改为

来自

snapshot.data.documents[index].get("image")

收件人

snapshot.data["image"]

注意,我没有对其进行测试。因此,它可能行不通。