简体   繁体   中英

Flutter firebase retrieve My Conversations List collection

So i have created a chat app and i'm trying to retrieve the conversations created by user with other users, i'm not sure if i'm doing it right?

here is the code is there a way, thanks !

    class MessageLists extends StatelessWidget {
  final String currentUserId;
  final String userId;
  final String groupChatId;
  final String peerId;

  MessageLists({this.currentUserId, this.userId, this.groupChatId, this.peerId});

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: Firestore.instance
          .collection('messages')
          .getDocuments(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: LoadingWidget(),
          );
        }
        return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (contex, index) {
            return ListTile(
              leading: CircleAvatar(backgroundImage: AssetImage('assets/images/user.jpg'),),
              title: Text(currentUserId),
              subtitle: Text('Text Here'),
              trailing: Icon(Icons.arrow_right),
            );
          },
        );
      },
    );
  }
}

my Firestore collection1 my Firestore collection2

You can try using a StreamBuilder : https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html

StreamBuilder(
  stream: Firestore.instance.collection('messages').snapshots(),
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    if (snapshot.hasError) return Text('Error: ${snapshot.error}');
    switch (snapshot.connectionState) {
      case ConnectionState.waiting:
        return CircularProgressIndicator();
        break;
      default:
        final document = snapshot.data;
        print(document);
    }
  },
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM