我正在尝试从Firestore检索单个文档并将其转换为模型的对象。
型号:
class User {
final String uid;
final String email;
final String nome;
final String cognome;
final String displayName;
final Ruolo ruolo;
String idSquadra;
DateTime scadenzaCertificato;
User(
{this.uid,
this.ruolo,
this.email,
this.nome,
this.cognome,
this.displayName,
this.idSquadra,
this.scadenzaCertificato});
static User fromSnapshot(DocumentSnapshot snapshot) {
return User(
uid: snapshot.documentID,
email: snapshot['email'],
nome: snapshot['nome'],
cognome: snapshot['cognome'],
displayName: snapshot['displayName'],
ruolo: snapshot['ruolo'],
idSquadra: snapshot['idSquadra'],
scadenzaCertificato: snapshot['scadenzaCertificato']);
}
}
检索:
import 'package:alley_app/model/data.dart';
import 'package:alley_app/model/user.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class DatabaseService {
final CollectionReference usersCollection = Firestore.instance.collection('users');
User getUser(String uid) async {
return User.fromSnapshot(await usersCollection.document(uid).get());
}
}
getUser
函数不断给我一个错误
不能从方法“ getUser”返回“用户”类型的值,因为它的返回类型为“用户”。 dart(return_of_invalid_type)
导入是正确的,用户对象是相同的,我在做什么错了?
答案 0 :(得分:1)
该方法具有async
修饰符,因此该方法应返回一个Future,在这种情况下应为Future。
Future<User> getUser(String uid) async {
return User.fromSnapshot(await usersCollection.document(uid).get());
}