flutter null检查运算符用于null值,导致小部件失败



D/EGL_emulation(9253):app_time_stats:平均值=1019.37ms最小值=4.61ms最大值=25327.36ms计数=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

下面的代码是我的全部代码,我不知道哪个值为null,我能帮我解决这个问题吗?

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

您的snapshot可能是null,并且您在其上使用!(searchedData!),因此更改此项:

final searchedData = snapshot.data;

到此:

final searchedData = snapshot.data ?? [];

对于您的下一期(RangeError),您在其他列表上使用listview的索引,您的listview的指数是针对userData的,但您在searchedData上使用的,这不是同一个列表。

此外,您还忘记为第二个StreamBuilder设置if else条件。做和第一次完全一样的事。

相关内容

  • 没有找到相关文章

最新更新