firebase - RangeError(索引):无效值:有效值范围为空:0 | 颤振火力基地
问题描述
嘿那里。所以我的问题是,当页面加载时它会显示以下错误 1-2 秒,但随后数据显示完美。它不应该发生,因为我已经向“FutureBuilder”提供了“initialData”。谁能告诉我我错过了什么。
注意:我不能使用 if(snapshot.connectionState == ConnectionState.waiting) return CircleProgressIndicator();
,因为我正在使用 PageView.builder 并且当我移动到第二页时它会再次刷新并自动进入第一页
═══════ Exception caught by widgets library ═══════════════════════════════════
The following RangeError was thrown building FutureBuilder<List<dynamic>>(dirty, state: _FutureBuilderState<List<dynamic>>#bad68):
RangeError (index): Invalid value: Valid value range is empty: 0
The relevant error-causing widget was
FutureBuilder<List<dynamic>>
When the exception was thrown, this was the stack
#0 List.[] (dart:core-patch/growable_array.dart:177:60)
#1 _TasksState.build.<anonymous closure>
#2 _FutureBuilderState.build
#3 StatefulElement.build
#4 ComponentElement.performRebuild
...
════════════════════════════════════════════════════════════════════════════════
这是我的代码:
body: FutureBuilder(
initialData: [],
future: Future.wait([
getRequests(),
getCNIC(),
]),
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
indexLength = snapshot.data[0].length;
cnicCheck = snapshot.data[1];
if (indexLength == 0)
return SizedBox(
child: Center(
child: Text(
"No Buyer Requests",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: kPrimaryColor),
),
),
);
return SizedBox(
child: PageView.builder(
itemCount: indexLength,
physics: PageScrollPhysics(),
controller: PageController(viewportFraction: 1.0),
onPageChanged: (int index) => setState(() => _index = index),
itemBuilder: (_, i) {
return SingleChildScrollView(
child: Card(
margin: EdgeInsets.all(10),
child: Wrap(
children: <Widget>[
ListTile(
leading: CircleAvatar(
backgroundColor: kPrimaryColor.withOpacity(0.8),
backgroundImage:
AssetImage('assets/images/nullUser.png'),
child: snapshot.data[0][i]['PhotoURL'] != null
? ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.network(
snapshot.data[0][i]['PhotoURL'],
width: 50,
height: 50,
fit: BoxFit.cover,
),
)
: ClipRRect(
borderRadius: BorderRadius.circular(50),
child: Image.asset(
'assets/images/nullUser.png',
width: 50,
height: 50,
fit: BoxFit.cover,
),
)),
title: Text(
snapshot.data[0][i]['Email'],
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
color: Colors.black.withOpacity(0.7),
),
),
subtitle: Text(
snapshot.data[0][i]['Time'],
style: TextStyle(
color: Colors.black.withOpacity(0.6)),
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
color: Colors.grey[200],
),
padding: EdgeInsets.all(10),
child: Text(
snapshot.data[0][i]['Description'],
style: TextStyle(
color: Colors.black.withOpacity(0.6)),
),
),
SizedBox(
height: 8,
),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(Icons.category_outlined),
title: Text(
'Category : ${snapshot.data[0][i]['Category']}',
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
),
),
SizedBox(height: 8),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(Icons.location_pin),
title: Text(
snapshot.data[0][i]['Location'],
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
),
),
SizedBox(height: 8),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(
Icons.attach_money,
color: kGreenColor,
),
title: Text(
'Budget : Rs.${snapshot.data[0][i]['Budget']}',
style: TextStyle(
fontSize: 14,
color: kGreenColor,
),
),
),
),
SizedBox(height: 8),
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(5)),
border:
Border.all(color: Colors.grey[300])),
child: ListTile(
leading: Icon(Icons.timer),
title: Text(
'Duration : ${snapshot.data[0][i]['Duration']}',
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
),
),
SizedBox(
height: 35,
),
RaisedButton(
padding:
EdgeInsets.symmetric(vertical: 10),
child: Text('Send Offer'),
textColor: Colors.white,
color: Colors.green,
onPressed: () {
if (cnicCheck == "verified") {
print(cnicCheck);
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => SendOffer(snapshot.data[0][i].id),
),
);
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => VerifyCNIC(),
),
);
}
},
),
SizedBox(
height: 15,
),
Center(
child: Text(
"${i + 1}/$indexLength",
style: TextStyle(fontSize: 13),
),
),
],
),
),
],
),
),
);
},
),
);
},
),
解决方案
您的问题:您尝试访问不存在的列表索引处的值。这是因为,提供的初始数据是一个空列表。当“FutureBuilder”第一次被调用时,它包含的所有代码都使用初始数据执行。所以前两行会抛出一个错误消息,因为你试图从列表中获取第一个和第二个元素,而实际上它是空的并且没有第一个或第二个元素。
解决方案:您必须自己决定如何向用户显示数据仍在加载。但是,如果您的列表可能为空或可能仅包含一个元素,则在尝试从列表中访问这些项目之前,您必须检查是否是这种情况。
所以只需使用:
if (snapshot.data.length > 1)
在执行之前:
indexLength = snapshot.data[0].length;
cnicCheck = snapshot.data[1];
我真的希望我能帮忙:)
推荐阅读
- grails - Grails Spring Security Shiro,如何在无需注销重新登录的情况下刷新权限?
- julia - 如何计算我的主要代码的时间?
- android - How can I use Android expansion files using App Bundle?
- sql-server - 如何在不为表指定列名的情况下将动态数据透视的结果存储到临时表中?
- azure - Microsoft Azure 数据仓库:平面表或星型架构
- javascript - 如何仅打印日期而不是完整日期?
- php - ErrorException 未定义索引:HTTP_X_REQUESTED_WITH
- python - 如何将 rpy2.robjects.vectors.DataFrame 转换为 csv?
- javascript - 在数组数组中按 id 查找对象的索引
- git - 如何避免将文件夹推送到 git repo