对空值使用了flutter空值检查运算符,导致小部件失败

gudnpqoy  于 2022-12-05  发布在  Flutter
关注(0)|答案(1)|浏览(153)

D/EGL仿真(9253):应用程序时间统计:平均值= 1019.37毫秒最小值= 4.61毫秒最大值= 25327.36毫秒计数=25
生成时引发了以下RangeError

StreamBuilder<List<showSummaryReport>>(dirty, state: _StreamBuilderBaseState<List<showSummaryReport>, AsyncSnapshot<List<showSummaryReport>>>#a2976):
RangeError (index): Invalid value: Valid value range is empty: 1

导致错误的相关小部件为:

StreamBuilder<List<showSummaryReport>> StreamBuilder:file:///C:/Users/limji/StudioProjects/fyp/lib/report/SummaryReport/selectedSummary.dart:225:38
When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:264:36)
#1      _selectedSummaryState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:fyp/report/SummaryReport/selectedSummary.dart

下面的代码是我的整个代码,我不知道哪个值是空的,我可以有一些帮助修复这个?

Visibility(
              visible: gotChoice,
              child: StreamBuilder<List<showSummaryReport>>(
                  stream: read('${widget.expChoice.toString()} Sales'),
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                    if (snapshot.hasError) {
                      return Center(
                        child: Text("some error occured"),
                      );
                    }
                    if (snapshot.hasData) {
                      final userData = snapshot.data;
                      return Expanded(
                        child: ListView.builder(
                            itemCount: userData!.length,
                            itemBuilder: (context, index) {
                              final service = userData[index];
                              return StreamBuilder<List<showSummaryReport>>(
                                stream: read('${test} Sales'),
                                builder: (context, snapshot) {
                                  final searchedData = snapshot.data ?? [];
                                  final searched = searchedData[index];
                                  return Column(
                                    children: [
                                      ListTile(
                                          onTap: () {
                                            Navigator.push(context, MaterialPageRoute(builder: (context)=>
                                                selectedDetailReport(detail: '${service.serviceName} Sales',
                                                  month: widget.expChoice.toString(),)));
                                          },
                                          title: Card(
                                            color: 'CAF0F8'.toColor(),
                                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
                                            child: Table(
                                              // border: TableBorder(bottom: BorderSide(color: '03045E'.toColor(), width: 1)),
                                              children: [
                                                TableRow(children: [SizedBox(height: 10,), SizedBox(height: 10,)]),
                                                TableRow(
                                                    children: [
                                                      Padding(
                                                        padding: const EdgeInsets.fromLTRB(30,0,0,0),
                                                        child: Text('Service Name :',style: TextStyle(fontFamily: 'MonSemi', fontSize: 17)),
                                                      ),
                                                      Center(child: Text(service.serviceName.toString(),style: TextStyle(fontFamily: 'MonSemi', fontSize: 17))),
                                                    ]
                                                ),
                                                TableRow(children: [SizedBox(height: 10,), SizedBox(height: 10,)]),
                                                TableRow(
                                                    children: [
                                                      Padding(
                                                        padding: const EdgeInsets.fromLTRB(30,0,0,0),
                                                        child: Text('Current Month Sales :',style: TextStyle(fontFamily: 'MonSemi', fontSize: 17)),
                                                      ),
                                                      Center(child: Text(service.sales.toString(),style: TextStyle(fontFamily: 'MonSemi', fontSize: 17))),
                                                    ]
                                                ),
                                                TableRow(children: [SizedBox(height: 10,), SizedBox(height: 10,)]),
                                                TableRow(
                                                    children: [
                                                      Padding(
                                                        padding: const EdgeInsets.fromLTRB(30,0,0,0),
                                                        child: Text('${test} Sales :',style: TextStyle(fontFamily: 'MonSemi', fontSize: 17)),
                                                      ),
                                                      Center(child: Text('${searched.sales}',style: TextStyle(fontFamily: 'MonSemi', fontSize: 17))),
                                                    ]
                                                ),
                                                TableRow(children: [SizedBox(height: 10,), SizedBox(height: 10,)]),
                                                TableRow(
                                                    children: [
                                                      Padding(
                                                        padding: const EdgeInsets.fromLTRB(30,0,0,0),
                                                        child: Text('${test} Sales :',style: TextStyle(fontFamily: 'MonSemi', fontSize: 17)),
                                                      ),
                                                      Center(child: Text('${searched.sales}',style: TextStyle(fontFamily: 'MonSemi', fontSize: 17))),
                                                    ]
                                                ),
                                                TableRow(children: [SizedBox(height: 10,), SizedBox(height: 10,)]),
                                              ],
                                            ),
                                          ),
                                      ),
                                    ],
                                  );
                                }
                              );
                            }),
                      );
                    }
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }),
            ),
wz8daaqr

wz8daaqr1#

您的snapshot可能是null,而您在其上使用了!searchedData!),因此请更改此设置:

final searchedData = snapshot.data;

更改为:

final searchedData = snapshot.data ?? [];

对于您的下一个问题(RangeError),您正在使用其他列表上的列表视图索引,您的列表视图索引是针对userData的,但您正在使用searchedData上的索引,这不是同一个列表。
您还忘了为第二个StreamBuilder设置if else条件请执行与第一个StreamBuilder完全相同操作

相关问题