gpt4 book ai didi

firebase - 如何在 Flutter 中连接两个 Firestore 集合中的数据?

转载 作者:行者123 更新时间:2023-12-02 10:35:44 28 4
gpt4 key购买 nike

我在 Flutter 中有一个使用 Firestore 的聊天应用程序,并且我有两个主要集合:

  • chats,以自动 ID 为键,并具有 messagetimestampuid 字段.
  • users,以 uid 为键,并具有 name 字段

在我的应用程序中,我使用以下小部件显示消息列表(来自 messages 集合):

class ChatList extends StatelessWidget {
@override
Widget build(BuildContext context) {
var messagesSnapshot = Firestore.instance.collection("chat").orderBy("timestamp", descending: true).snapshots();
var streamBuilder = StreamBuilder<QuerySnapshot>(
stream: messagesSnapshot,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> querySnapshot) {
if (querySnapshot.hasError)
return new Text('Error: ${querySnapshot.error}');
switch (querySnapshot.connectionState) {
case ConnectionState.waiting: return new Text("Loading...");
default:
return new ListView(
children: querySnapshot.data.documents.map((DocumentSnapshot doc) {
return new ListTile(
title: new Text(doc['message']),
subtitle: new Text(DateTime.fromMillisecondsSinceEpoch(doc['timestamp']).toString()),
);
}).toList()
);
}
}
);
return streamBuilder;
}
}

但现在我想显示每条消息的用户名(来自 users 集合)。

我通常称之为客户端连接,尽管我不确定 Flutter 是否有具体的名称。

我找到了一种方法来执行此操作(我已在下面发布),但想知道是否有另一种/更好/更惯用的方法在 Flutter 中执行此类操作。

那么:在 Flutter 中查找上述结构中每条消息的用户名的惯用方法是什么?

最佳答案

你可以像这样用 RxDart 做到这一点.. https://pub.dev/packages/rxdart

import 'package:rxdart/rxdart.dart';

class Messages {
final String messages;
final DateTime timestamp;
final String uid;
final DocumentReference reference;

Messages.fromMap(Map<String, dynamic> map, {this.reference})
: messages = map['messages'],
timestamp = (map['timestamp'] as Timestamp)?.toDate(),
uid = map['uid'];

Messages.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);

@override
String toString() {
return 'Messages{messages: $messages, timestamp: $timestamp, uid: $uid, reference: $reference}';
}
}

class Users {
final String name;
final DocumentReference reference;

Users.fromMap(Map<String, dynamic> map, {this.reference})
: name = map['name'];

Users.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data, reference: snapshot.reference);

@override
String toString() {
return 'Users{name: $name, reference: $reference}';
}
}

class CombineStream {
final Messages messages;
final Users users;

CombineStream(this.messages, this.users);
}

Stream<List<CombineStream>> _combineStream;

@override
void initState() {
super.initState();
_combineStream = Observable(Firestore.instance
.collection('chat')
.orderBy("timestamp", descending: true)
.snapshots())
.map((convert) {
return convert.documents.map((f) {

Stream<Messages> messages = Observable.just(f)
.map<Messages>((document) => Messages.fromSnapshot(document));

Stream<Users> user = Firestore.instance
.collection("users")
.document(f.data['uid'])
.snapshots()
.map<Users>((document) => Users.fromSnapshot(document));

return Observable.combineLatest2(
messages, user, (messages, user) => CombineStream(messages, user));
});
}).switchMap((observables) {
return observables.length > 0
? Observable.combineLatestList(observables)
: Observable.just([]);
})
}

对于 rxdart 0.23.x

@override
void initState() {
super.initState();
_combineStream = Firestore.instance
.collection('chat')
.orderBy("timestamp", descending: true)
.snapshots()
.map((convert) {
return convert.documents.map((f) {

Stream<Messages> messages = Stream.value(f)
.map<Messages>((document) => Messages.fromSnapshot(document));

Stream<Users> user = Firestore.instance
.collection("users")
.document(f.data['uid'])
.snapshots()
.map<Users>((document) => Users.fromSnapshot(document));

return Rx.combineLatest2(
messages, user, (messages, user) => CombineStream(messages, user));
});
}).switchMap((observables) {
return observables.length > 0
? Rx.combineLatestList(observables)
: Stream.value([]);
})
}

关于firebase - 如何在 Flutter 中连接两个 Firestore 集合中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061225/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com