首页 > 解决方案 > 如何使用 firebase firestore 最小化我的颤振应用程序中的 RAM 消耗?

问题描述

我有一个使用 Flutter 制作的应用程序,它连接到 Firebase Firestore 并读取大量文档。每次用户在应用程序的视图之间移动并返回到显示这些文档的视图时,对 RAM 空间的使用会更大。我的代码中有一个 StreamBuilder,它在每次加载视图时调用集合。我的问题是:有没有办法在每次加载视图时最小化 RAM 的使用?,因为此应用程序每次在 RAM 内存很少的设备上达到其 RAM 内存空间限制时都会崩溃。请我需要帮助!这是我的 StreamBuilder 代码:

StreamBuilder<QuerySnapshot>(
    stream: this.rolUsuario != 'Digitador'
        ? this
            .fs
            .collection('Encuestas')
            .where('OrganizacionPerteneciente',
                isEqualTo: this.organizacionPerteneciente)
            .orderBy('FechaCreacion', descending: true)
            .snapshots()
        : this
            .fs
            .collection('Encuestas')
            .where('OrganizacionPerteneciente',
                isEqualTo: this.organizacionPerteneciente)
            .where('Asignada', arrayContains: this.uID.toString())
            .orderBy('FechaCreacion', descending: true)
            .snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) return Text('Error: ${snapshot.error}');
      switch (snapshot.connectionState) {
        case ConnectionState.waiting:
          return Center(
            child: CircularProgressIndicator(),
          );
        default:
          this.encuestas = new List<Encuesta>();
          snapshot.data.documents.forEach((encuesta) {
            this.encuestas.add(Encuesta.parse(encuesta.data));
          });

          return snapshot.data.documents.length == 0
              ? Center(
                  child: Text(
                    this.rolUsuario != 'Digitador'
                        ? 'Aún no se han creado encuestas en la plataforma.\nPara agregar nuevas encuestas ve a https://surveyflow.tk desde un computador.'
                        : 'Aún no tienes asignada ninguna encuesta para rellenar.',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 18,
                    ),
                  ),
                )
              : ListView.separated(
                  itemCount: snapshot.data.documents.length,
                  separatorBuilder: (context, index) => Divider(
                    color: Colors.blueGrey[100],
                  ),
                  padding: EdgeInsets.only(top: 15),
                  itemBuilder: (context, index) => Padding(
                    padding: EdgeInsets.symmetric(horizontal: 5),
                    child: ListTile(
                      selected: false,
                      title: Text(
                        snapshot.data.documents[index]['Nombre'],
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      dense: true,
                      subtitle: RichText(
                        text: TextSpan(
                            text: snapshot.data.documents[index]
                                    ['Descripcion'] +
                                '\n',
                            style: TextStyle(
                              color: Colors.blueGrey,
                            ),
                            children: <TextSpan>[
                              TextSpan(
                                  text: 'Fecha de Creación: ' +
                                      imprimirFecha(
                                        snapshot.data.documents[index]
                                            ['FechaCreacion'],
                                      ),
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontWeight: FontWeight.bold,
                                  )),
                            ]),
                      ),
                      leading: CircleAvatar(
                        child: Text(snapshot
                            .data.documents[index]['Encuestados']
                            .toString()),
                      ),
                      isThreeLine: false,
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => RellenarEncuestaView(
                              encuestaEditar: Encuesta.parse(
                                  snapshot.data.documents[index]),
                              isAndroid: this.widget.isAndroid,
                              tipoEdicion: 'nuevo',
                              authInstance: this.widget.authInstance,
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                );
      }
    },
  ),

标签: firebasefluttergoogle-cloud-firestore

解决方案


推荐阅读