【问题标题】:FutureBuilder and ListView.builderFutureBuilder 和 ListView.builder
【发布时间】:2021-01-07 12:30:08
【问题描述】:

有人可以向我解释如何使用位于 FutureBuilder 内部的 ListView.Builder 中的 itemCount。

目前我的 FirebaseCloud 商店只有一个 Document ,而我的应用正在返回一个无限的文档列表,

我尝试使用itemCount: snapshot.data.documents.length,

但是,得到错误:Class 'DocumentSnapshot' has no instance getter 'documents'. Receiver: Instance of 'DocumentSnapshot' Tried calling: documents

编辑如果有多个与 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('okkk');
                        print(manuais.data()['nome']);
                      } else {
                        print('nopeeeee');
                      }
                      return Container(
                        padding: EdgeInsets.all(16),
                        child: ListView.builder(

                            itemCount: snapshot.data.documents.length,

                            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');
                                              }),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              );
                            }),
                      );
                    })))),

````

【问题讨论】:

    标签: database firebase flutter dart google-cloud-firestore


    【解决方案1】:

    你必须这样做:

    itemCount : 1
    

    由于您只检索一个文档,当您这样做时:

    FirebaseFirestore.instance
                            .collection('users')
                            .doc(uid)
                            .get()
    

    以上将始终检索一个文档,因为文档 id 始终是唯一的。

    【讨论】:

    • 哇,好用,但我如何在列表中显示此集合的另一个文档?
    • 你需要做FirebaseFirestore.instance.collection("users").get()然后在itemCountsnapshot.data.docs.length
    • 但是,在 firebase cloud store db 上,我无法添加 2 个具有相同 UID 的文档
    • 与字段相同的 uid 但与文档 id 不同的 uid
    【解决方案2】:

    你需要像这样获得长度。

    itemCount: snapshot.data.data().length,
    

    【讨论】:

    • 然后我得到:类'DocumentSnapshot'没有实例获取器'length'。接收方:“DocumentSnapshot”实例尝试调用:长度
    • 你可以试试snapshot.data.data().length 吗?
    • 我做了:snapshot.data.data().length; ,我没有收到错误,但是我的列表仍然无穷大
    • 我很糟糕,我得到了一个错误:```` 类'DocumentSnapshot'没有实例方法'call'。接收方:'DocumentSnapshot' 的实例尝试调用:call()```` 当我更改为 itemCount:snapshot.data().length;
    • 你是什么意思“我的列表仍然无穷大”?
    【解决方案3】:
    if (snapshot.hasData) {
                            print('okkk');
                            print(manuais.data()['nome']);
    return Container(
                            padding: EdgeInsets.all(16),
                            child: ListView.builder(
    
                                itemCount: snapshot.data.documents.length,
    
                                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') ...(all your brackets go here)
        
             }
    

    【讨论】:

    • 尝试在snapshot.hasData的if条件下返回
    • 我不明白,我用来测试数据是否通过的 if 条件与问题有关?
    猜你喜欢
    • 2019-11-03
    • 2020-11-10
    • 2021-11-19
    • 2021-10-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    相关资源
    最近更新 更多