【发布时间】:2019-01-26 20:15:44
【问题描述】:
我正在开发一个 Flutter 应用,遇到一个问题就停下来了,希望你们能帮助我。
首先,英语不是我的第一语言,所以很抱歉有任何错别字。
我正在编写一个简单的“管理个人资料”屏幕。当用户在我的应用上注册时,我会向他展示一个 Scaffold,其中包含一些必须填写的字段,例如姓名、姓氏、电子邮件等。
我正在使用 Firestore 数据库,这就是我调用的异步方法来检索“用户”对象:
Future<Usuario> getUsuarioLogado() async {
Cartao cartao;
String email = await auth.currentUser();
String nome, sobrenome, cpf, numeroCartao, nomeCartao;
DocumentReference usuarioReference =
Firestore.instance.document('Usuarios/' + email);
usuarioReference.get().then((datasnapshot) {
if (datasnapshot.exists) {
nome = datasnapshot.data['nome'];
sobrenome = datasnapshot.data['sobrenome'];
cpf = datasnapshot.data['cpf'];
CollectionReference cartaoReference =
Firestore.instance.collection('Usuarios/' + email + '/Cartao');
cartaoReference.getDocuments().then((cartoes) {
List listCartoes = cartoes.documents;
nomeCartao = listCartoes[0].data['nome'];
numeroCartao = listCartoes[0].data['numero'];
cartao = new Cartao(nomeCartao, numeroCartao);
return new Usuario(cpf, nome, sobrenome, email, cartao);
}).catchError((e) {
print('#ANTLOG_GET_CARTAO: ' + e);
});
}
return new Usuario(cpf, nome, sobrenome, email, cartao);
}).catchError((e) {
print('#ANTLOG_GET_USUARIO: ' + e);
});
}
上面的代码在我调试时工作,但在我需要时它不返回对象。现在是我的屏幕正在构建自己的时刻。
在阅读了一些论坛后,我尝试返回一个 FutureBuilder,它只会在“Usuarios”(用户)不为空时显示内容。
return FutureBuilder<Usuario>(
future: perfil.getUsuarioLogado(),
builder: (BuildContext context, AsyncSnapshot<Usuario> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
print('#ANTLOG: ${snapshot.data}');
return scaffold;
} else
return Text('Loading...');
},
);
下面的代码给我一个空值:
print('#ANTLOG: ${snapshot.data}');
我想知道如何强制屏幕等到我拥有我的“Usuarios”对象。似乎 FutureBuilder 不工作或者我做错了什么。
我需要你的帮助。
【问题讨论】:
-
尝试使用
snapshot.connectionState == done代替snapshot.hasData -
@RémiRousselet,现在屏幕总是在
Loading...屏幕中 -
您的加载很可能有错误。因此
hasData总是假的 -
我的快照有时会检索数据,但调用了
return方法并且它没有数据。我可以添加一些侦听器来获取快照对象,以便我可以使用检索到的数据重新加载我的 Scaffold 吗?
标签: firebase firebase-realtime-database async-await flutter google-cloud-firestore