我想知道如何显示多个与同一USER UID相关的文档
目前,我的APP仅显示一个pdf文档
我可以怎么做才能显示同一用户的多个文档??? 令我困惑的是,这种逻辑应该如何工作,是否可以在没有SQL的情况下做到这一点?
我只知道如何使用用户UID获取单个文档。
我的代码:
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
FirebaseFirestore firestore = FirebaseFirestore.instance;
FirebaseAuth auth = FirebaseAuth.instance;
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
final User user = auth.currentUser;
final uid = user.uid;
final tabs = [
Center(
child: (Scaffold(
body: FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.doc(uid)
.get(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.data == null)
return CircularProgressIndicator();
DocumentSnapshot manuais = snapshot.data;
if (snapshot.hasData) {
print('ok');
print(manuais.data()['nome']);
} else {
print('nopeeeee');
}
return Container(
padding: EdgeInsets.all(16),
child: ListView.builder(
itemCount : 1,
itemBuilder: (context, index) {
DocumentSnapshot manuais = snapshot.data;
return Card(
color: Colors.grey[250],
child: Container(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
new Image.asset(
'Images/pdflogo.png',
width: 32,
),
Center(
child: Text(
(manuais.data()['nome'].toString()),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 16),
),
),
ButtonBar(
children: <Widget>[
FlatButton(
child: const Text(
'Compartilhar / Download'),
onPressed: () async {
var request = await HttpClient()
.getUrl(Uri.parse(manuais
.data()['documento']));
var response =
await request.close();
Uint8List bytes =
await consolidateHttpClientResponseBytes(
response);
await Share.file(
'ESYS AMLOG',
'Manual.pdf',
bytes,
'image/jpg');
}),
],
),
],
),
),
);
}),
);
})))),
答案 0 :(得分:2)
在一个集合中不能有多个具有相同ID的文档:一个文档ID在一个集合中必须是唯一的。
就您而言,如果我正确理解,您可以为每个user
文档创建一个subcollection,例如
-- users (collection)
-- DTB2.... (user document)
-- pdfDocs (subcollection)
-- JH8766ng.... (pdf document, with auto-generated ID)
fields: nome: "PDF1", creationDate: ..., author: .... (for example)
-- 65rt6b8H.... (pdf document, with auto-generated ID)
fields: nome: "PDF2", creationDate: ..., author: ....
您将使用以下命令查询所有文档:
FirebaseFirestore.instance
.collection('users')
.doc(uid)
.collection('pdfDocs')
.get();