如何减少扑朔迷离的Firestore读取次数

时间:2020-09-17 08:52:53

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

我正在构建一个使用streambuilder从Firestore提取数据的应用。

我在Firestore上只写了6个文档,但是我仅用一部电话就可以读取350多次,请问如何减少这种情况?

我也看到了一篇有关如何使用Cache减少读取次数的文章,但这是针对Java(Android)的,我不知道该如何在flutter中使用它。

StreamBuilder<QuerySnapshot>(
              stream: Firestore.instance
                  .collection("free")
                  .where("created", isGreaterThan: DateTime.now().subtract(Duration(hours: 24)))
                  .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Text('.....Loading');
                }
                return ListView.builder(
                    scrollDirection: Axis.vertical,
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index) {
                      DocumentSnapshot free =
                          snapshot.data.documents[index];
                      return Container(
                        padding: EdgeInsets.all(10),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Column(
                              children: [
                                Text(
                                  '${free['safe']['time']}',
                                  style: TextStyle(fontSize: 18),
                                ),
                                SizedBox(
                                  height: 3,
                                ),
                                Text(
                                  '${free['safe']['date']}',
                                  style: TextStyle(fontSize: 13),
                                ),
                              ],
                            ),

// SizedBox(width:20,),

                            SizedBox(
                              width:
                                  0.5 * MediaQuery.of(context).size.width,
                              child: Column(
                                crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    '${free['safe']['league']}',
                                    style: TextStyle(fontSize: 15),
                                  ),
                                  SizedBox(
                                    height: 2,
                                  ),
                                  Text(
                                    '${free['safe']['home']}',
                                    style: TextStyle(
                                        fontSize: 18,
                                        fontWeight: FontWeight.w500),
                                  ),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Text(
                                    'vs ${free['safe']['away']}',
                                    style: TextStyle(
                                        fontSize: 18,
                                        fontWeight: FontWeight.w500),
                                  ),
                                  SizedBox(
                                    height: 3,
                                  ),
                                  Text(
                                    '${free['safe']['tip']}',
                                    style: TextStyle(
                                        fontSize: 18,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.redAccent),
                                  ),
                                ],
                              ),
                            ),

// SizedBox(width:20,),

                            Column(
                              children: [
                                Text(
                                  '${free['safe']['odd']}',
                                  style: TextStyle(fontSize: 18),
                                ),
                                SizedBox(
                                  height: 4,
                                ),
                                Builder(
                                  builder: (context) {
                                    if (free['safe']['result'] == 'win') {
                                      return Column(
                                        children: [
                                          Icon(
                                            Icons.check_circle,
                                            color: Colors.green,
                                          ),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          Text(
                                            'WON',
                                            style: TextStyle(
                                                color: Colors.green,
                                                fontWeight:
                                                    FontWeight.bold),
                                          )
                                        ],
                                      );
                                    } else if (free['safe']['result'] ==
                                        'loss') {
                                      return Column(
                                        children: [
                                          Icon(
                                            Icons.close,
                                            color: Colors.red,
                                          ),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          Text(
                                            'LOSS',
                                            style: TextStyle(
                                                color: Colors.red,
                                                fontWeight:
                                                    FontWeight.bold),
                                          )
                                        ],
                                      );
                                    } else {
                                      return Text('...');
                                    }
                                  },
                                ),
                              ],
                            ),
                          ],
                        ),
                      );
                    });
              }),

0 个答案:

没有答案