我需要从新创建的文档中获取并保存文档ID.如何从Future返回此id,或者如何将docRef.id添加到客户端提供程序?
Future<void> saveNewClient(client) async {
try {
DocumentReference docRef = await _db.collection('client').add(client);
client.updateClientId(docRef.id); <<<< CLIENT PROVIDER
} catch (e) {
print(e);
}
return;
}
这段代码不会抛出错误,但它永远不会到达client.updateClientId(docRef.id)行.保存工作,所以我应该能够得到docRef. id.
为什么要跳过这一行?
UPDATE:个 既然我已经从saveNewClient函数获得了文档id,我该如何使用该变量将id添加到客户端提供程序中呢?以下是代码:
Future<String?> clientId =
firestoreService.saveNewClient(toMap(newClient));
ref.read(clientNotifierProvider.notifier).updateClientId(clientId);
因为客户ID是future 的,所以我需要等待它被填充.我该怎么做呢? 谢谢