flutter null check 运算符用于 null 值,导致 widget 失败

flutter null check operator was used on a null value, causes widget failure

提问人:Lim 提问时间:12/3/2022 最后编辑:eamirho3einLim 更新时间:12/4/2022 访问量:163

问:

D/EGL_emulation( 9253): app_time_stats: avg=1019.37ms min=4.61ms max=25327.36ms count=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(),
                    );
                  }),
            ),
Flutter Dart 异常 null widget

评论

0赞 Munsif Ali 12/3/2022
检查调试控制台,您将获得对导致此问题的值的引用

答:

0赞 eamirho3ein 12/3/2022 #1

你的可能是,你用在它(),所以改变这个:snapshotnull!searchedData!

final searchedData = snapshot.data;

对此:

final searchedData = snapshot.data ?? [];

对于您的下一个问题(RangeError),您在其他列表上使用listview的索引,您的listview的索引是用于,但您正在使用它,这些不是同一个列表。userDatasearchedData

此外,您忘记为第二个StreamBuilder设置条件。做与第一个完全相同的事情。if else

评论

0赞 Lim 12/3/2022
先生,我已经按照您的建议更改了代码,但它似乎又有问题了,但是对于范围,我已经编辑了错误代码供您查看
0赞 Lim 12/3/2022
但是先生,为什么我不能使用它,因为我想从 2 个集合中检索数据,这就是我使用相同索引的原因
0赞 eamirho3ein 12/3/2022
因为这些列表可能没有相同的项目。例如,像这里一样,userData 有值,但 searchedData 为空。
0赞 Lim 12/3/2022
呃,那我该如何解决它
0赞 eamirho3ein 12/3/2022
你需要看看 stream: read('${test} Sales'),它似乎效果不佳