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

Time:12-05

D/EGL_emulation( 9253): app_time_stats: avg=1019.37ms min=4.61ms max=25327.36ms count=25

The following RangeError was thrown building

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

The relevant error-causing widget was:

  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

The code below is my entire code, i don't know which value is null, can i have some help on fixing this ?

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(),
                    );
                  }),
            ),

CodePudding user response:

Your snapshot may be null and you use ! on it(searchedData!),so change this:

final searchedData = snapshot.data;

to this:

final searchedData = snapshot.data ?? [];

for your next issue(RangeError) you are using listview's index on other list, your listview's index is for userData but you are using that on searchedData, these are not the same list.

Also you forgot to set if else condition for second StreamBuilder. do exact same thing that you do for first one.

  • Related