0
votes

What I want to do is to retrieve data from Cloud Firestore just for user Logged into the app. I am using Auth Firebase and I built a method to retrieve some documents from a specific collection called "user" and it works and it show the right Name of the user logged and the user signed up into my form. Now I am collecting with another form another Firestore collection called "shippingAddress" and the documents into the "shippingAddress" collection should be show into the app just for the specific user logged into the app. but this is not happen, any user logged I got the same data from all user except name and email address. this is my code:

final databaseReference = Firestore.instance;

  Future<QuerySnapshot> getData() async {
    var firebaseUser = await FirebaseAuth.instance.currentUser();
    return await Firestore.instance
        .collection("user")
        .where("email", isEqualTo: firebaseUser.email)
        .getDocuments();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: getData(),
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return ListView.builder(
              shrinkWrap: true,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (BuildContext context, int index) {
                return Column(
                  children: <Widget>[
                    Stream(),
                  ],
                );
              });
        } else if (snapshot.data == null) {
          return Center(child: CircularProgressIndicator());
        }
        return Center(child: CircularProgressIndicator());
      },
    );
  }
}

class Stream extends StatefulWidget {
  @override
  _StreamState createState() => _StreamState();
}

class _StreamState extends State<Stream> {

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Firestore.instance.collection('shippingAddress').snapshots(),
      builder: (context, snapshot) {
//        if (!snapshot.hasData) return Text('Loading data please Wait');
        return Column(
          children: <Widget>[
            Container(
              height: 1000,
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: snapshot.data.documents.length,
                itemBuilder: (BuildContext context, int index) {
                  return Card(
                    color: Color(0xFF1f2032),
                    elevation: 15,
                    child: Container(
                      width: 60,
                      height: 60,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          
                          Text(
                            snapshot.data.documents[index]['alias'],
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        );
      },
    );
  }
}


please help I am stuck into this. here is the screenshot of my database: Screen of database

User collections

1
Right now you load all shipping addresses with stream: Firestore.instance.collection('shippingAddress').snapshots(). You'll need to write a query that selects only the shipping addresses for the current user. If that is giving you problems, edit your question to include a screenshot of a shippingAddress document. - Frank van Puffelen
I added the database screenshot - Wytex System
How is the document in that screenshot related to a user? If the document ID is the user's UID, then you can do var user = await FirebaseAuth.instance.currentUser(); Firestore.instance.collection('shippingAddress').document(user.uid).snapshots() - Frank van Puffelen
Ok I added user collections. - Wytex System
How do you know what user (in screenshot 2) the shipping address (in screenshot 1) belongs to? Firestore doesn't automatically add such relations, so if it exists somewhere it's because you added it. - Frank van Puffelen

1 Answers

1
votes
**can you share the Database image? 

if you want specific user information you need to use this method

 Firestore.instance
    .collection('talks')
    .document('document-name')
    .get()
    .then((DocumentSnapshot ds) {
  // use ds as a snapshot
});