是否可以在streambuilder中使用for循环?

时间:2019-05-03 17:22:32

标签: flutter google-cloud-firestore

我正在尝试使用streambuilder从Cloud Firestore中获取一些数据,并使用该数据返回一列,问题是当我尝试将for循环插入到构建器时(如代码块所示),它会提示我,构建器函数返回null,在删除for循环时不会发生。

问题是我需要使用for循环(或类似的循环)来实现所需的功能,因为我正在使用成对的数据(如代码块所示),因此第一个容器使用数据快照{{ 1}},第二个使用数据快照i。然后对于下一次迭代,i+1将是i,因此for循环就可以完成此工作。

我还为您提供了堆栈跟踪。预先感谢。

2
Container(
                        height: MediaQuery.of(context).size.height * 0.35,
                        child: StreamBuilder(
                          stream: Firestore.instance.collection('XXXXX').document('XXXXXXXXX').collection('XXXXXX').snapshots(),
                          builder: (context, snapshot) {
                            if(!snapshot.hasData) return 
                            Center(
                              child: Text('No documents to be shown')
                            );
                            final int largoDocumentos = snapshot.data.documents.length;
                            for(var i = 0; i > largoDocumentos; i = i + 2) {
                              return Column(  
                                children: <Widget>[
                                  Container(
                                    height: 130,
                                    child: Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: <Widget>[
                                          Material(
                                            color: Colors.blueGrey[50],
                                            child: InkWell(
                                              borderRadius: BorderRadius.circular(28.0),
                                              onTap: () {

                                              },
                                              child: Container(
                                                width: MediaQuery.of(context).size.width * 0.4,
                                                child: Column(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: <Widget>[
                                                    Text(snapshot.data.documents[i]['XXXXX'].toUpperCase(),
                                                        textAlign: TextAlign.center, 
                                                        style: TextStyle( 
                                                            fontSize: 18,
                                                            fontFamily: 'Montserrat',
                                                            fontWeight: FontWeight.bold,
                                                            color: Colors.grey)
                                                        ),
                                                      RadialProgress(0, 65.0, 20.0),
                                                  ],
                                                ),
                                              ),
                                            ),
                                          ),
                                          Material(
                                            color: Colors.blueGrey[50],
                                            child: InkWell(
                                              borderRadius: BorderRadius.circular(28.0),
                                              onTap: () {
                                              },
                                              child: Container(
                                                width: MediaQuery.of(context).size.width * 0.4,
                                                child: Column(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: <Widget>[
                                                    Text(snapshot.data.documents[i+1]['XXXXX'].toUpperCase(),
                                                        textAlign: TextAlign.center, 
                                                        style: TextStyle( 
                                                            fontSize: 18,
                                                            fontFamily: 'Montserrat',
                                                            fontWeight: FontWeight.bold,
                                                            color: Colors.grey)
                                                        ),
                                                      RadialProgress(30, 65.0, 20.0),
                                                  ],
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  SizedBox(height: 25,),
                                ],
                              );
                            }
                          },
                        ),
                      ),

0 个答案:

没有答案
相关问题