有没有一种方法可以在flutter中将Firestore数据获取到User模型类,然后将数据传递到屏幕



有一种方法可以获取firestore数据,例如"用户信息";进入flutter中的用户模型类?这是我的用户类别:


class Users {
final int id;
final String firstName;
final String lastName;
final String emailAddress;
final String imageProfile;
const Users({
this.id,
this.firstName,
this.lastName,
this.emailAddress,
this.imageProfile,
});
}
final Users currentUser = Users(
id: 'GET_DATA_FROM_FIRESTORE'/// firebaseUSer.currentUserUID
firstName: 'GET_DATA_FROM_FIRESTORE',
lastName: 'GET_DATA_FROM_FIRESTORE',
emailAddress: 'GET_DATA_FROM_FIRESTORE',
imageProfile: 'GET_DATA_FROM_FIRESTORE',
);

然后将具有CCD_ 1等的数据传递给其他小部件。我需要检索到这个类中的数据被创建到flutter中的SignuUp表单中,并被推送到firestore集合

我在项目中使用了这个。我希望这会有所帮助。

型号类别

class Message{
String id;
String message;
String senderId;
Timestamp timeStamp;
bool isMedia;
Message({this.id,this.message,this.senderId,this.timeStamp,this.isMedia});
factory Message.fromSnapshot(DocumentSnapshot snapshot){
return Message(
id:snapshot.id,
message: snapshot.data()['message'],
senderId: snapshot.data()['senderId'],
timeStamp: snapshot.data()['timeStamp'],
isMedia: snapshot.data()['isMedia'],
);
}
Map<String, dynamic> toJson() =>
{
'message': message,
'senderId': senderId,
'isMedia': isMedia,
'timeStamp': DateTime.now(),
};
}

服务类别

*初始

final FirebaseFirestore _fBaseFireStore = FirebaseFirestore.instance;
CollectionReference _collectionRef;
MessageService() {
_collectionRef = _fBaseFireStore.collection('Conversation');
}

*从消防仓库获取数据

Stream<List<Message>> getMessages(String conversationId) {
var ref = _collectionRef
.doc(conversationId)
.collection('messages');
return ref.snapshots().map(
(event) => event.docs.map((e) => Message.fromSnapshot(e)).toList());
}

*将数据写入消防仓库

Future<void> sendMessage(Message message, String conversationId) async {
var ref = _collectionRef.doc(conversationId).collection('messages');
await ref.add(message.toJson());
}

相关内容

最新更新