Kennedy Owusu
Kennedy Owusu

Reputation: 6070

Fetch user data from firestore and show them in profile screen using flutter

The issue here is that when I fetch the data, I am suppose to fetch it for the current user but it is rather fetching data for all users within that collection.

I have done reading and watched a number of videos for a possible solution but I can't seem to find how to do this. Your help is needed please. Thanks.

A excerpt of the bode is below.


File image;

  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

The collection name is members

Upvotes: 0

Views: 978

Answers (2)

3kdeveloper
3kdeveloper

Reputation: 822

First of All use a DocumentSnapshot Shown below:

StreamBuilder<DocumentSnapshot> 

Make a collection to get current user Profile data.

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

Remove ListView.builder

To get an Email Address use the below Line

Text('${streamSnapshot.data['Email Address']}'),

Here is the complete Article https://medium.com/@kamranktk807/fetch-user-data-from-firestore-and-show-them-in-profile-screen-using-flutter-609d2533e703

By the way I sol this problem with the help of a Professional Flutter Developer SHAKIR ZAHID [[email protected]].

Upvotes: 0

Anas
Anas

Reputation: 1073

Try like this, stream of your widget should be like this, as said above.

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

for length in Listview.builder, change it too

snapshot.data.length;

And last, All the data which you fetch data like this should change into

from:

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

To:

snapshot.data["image"]

Note I didn't test it. So, it might or might not work.

Upvotes: 1

Related Questions