当我尝试遍历 Flutter 中的嵌套对象时出现“Getter was called on null”错误

时间:2021-04-22 11:29:05

标签: flutter dart

我有一个看起来像这样的对象,

marketOptions: {option1: {optionValue: 'Yes', optionId: `${match.id}`+ '333'+ `1`}, option2:{optionValue: 'No', optionId: `${match.id}`+ '333'+ `2` }},

我想遍历“option1”、“option2”等并收集“optionValue”中的数据,我下面的第一次尝试是从我的 Firestore 数据库中获取对象并将其分配给一个名为“marketList”的变量,然后然后在列表中使用 Listview.builder 循环,但我不断收到错误“getter“optionValue”被调用为空”,我怎样才能实现我正在寻找的东西?

var marketList = thesnapshot.data()['marketOptions'];
var listCount = thesnapshot.data()['marketOptions'].length;



Container(height: listCount * MediaQuery.of(context).size.height * 11.8 / 100, child: ListView.builder(physics: const NeverScrollableScrollPhysics(),
                itemCount: listCount,
                itemBuilder: (context, index) {




                    return Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.center,



                            children: < Widget > [Container(height: MediaQuery.of(context).size.height * 10 / 100, child: Row(
                                        mainAxisAlignment: MainAxisAlignment.start,
                                        crossAxisAlignment: CrossAxisAlignment.center,



                                        children: < Widget > [


                                            Container(width: MediaQuery.of(context).size.width * 40 / 100, child: Text(marketList[index].optionValue, style: TextStyle(
                                                color: Color(0xffffffff),
                                                fontFamily: 'Montserrat',
                                                fontSize: MediaQuery.of(context).size.height * 1.8 / 100,
                                                fontWeight: FontWeight.w600))),

0 个答案:

没有答案