在listView flutter中给定特定数字时出错



当我在listView itemCount中给定一个特定的数字时,它会按预期工作,但在加载之前,它会给出一个错误。有人知道怎么解决这个问题吗?谢谢这是我正在使用的代码。这是给出的错误消息->NoSuchMethodError:对null调用了方法"[]"。接收方:null尝试调用。但是,当我使用snapshot.data.length.时,不会出现此错误

child: FutureBuilder(
future: data, //was getData() so if there are any errors change this and the PROBLEM WILL BE FIXED
builder: (context,
snapshot) { // getting items = snapshot.data[0]['Title']
try{
return ListView.separated(
separatorBuilder:(context, index) => Divider(
color: Colors.white,
) ,
padding: EdgeInsets.symmetric(vertical: 4),

itemCount: snapshot.data.length,
itemBuilder: (context, index){
return Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
offset: Offset(0.0, 0.75),
blurRadius: 7,
),
]
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.grey.shade300, width: 1.3),

),
child: NewsButton(
snapshot.data[index]['Image'],
snapshot.data[index]['Title'],
connectivity,
snapshot.data[index]['Link'],
context
),
),
);
});
} catch(exception){
checkConnection(context);
print(exception);
return CircularProgressIndicator();

}
}
) // getJson()
)

当你在第一个快照中给它一个特定的数字时,它没有数据,所以基本上它的数据是空的,当你写时

snapshot.data[index]['Link']

数据为null,您在null上调用[index],在获取数据后,错误就消失了,因为数据不再为null。

但是,当您使用快照数据长度时,它没有错误,因为当数据为null时,长度为零,当提取数据时,数据会更新,itemCount也是如此。

对于您的情况,只需检查数据是否为空(如果为空(。如果没有给出您想要的数字,则将项目计数设为零

试试这个,希望它对你有用。

child: FutureBuilder(
future: data, //was getData() so if there are any errors change this and the PROBLEM WILL BE FIXED
builder: (context,
snapshot) { // getting items = snapshot.data[0]['Title']
try{
return snapshot.sna.connectionState == ConnectionState.waiting
? Center(child: CircularProgressIndicator(),) 
: ListView.separated(
separatorBuilder:(context, index) => Divider(
color: Colors.white,
) ,
padding: EdgeInsets.symmetric(vertical: 4),

itemCount: snapshot.data.length,
itemBuilder: (context, index){
return Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
offset: Offset(0.0, 0.75),
blurRadius: 7,
),
]
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.grey.shade300, width: 1.3),

),
child: NewsButton(
snapshot.data[index]['Image'],
snapshot.data[index]['Title'],
connectivity,
snapshot.data[index]['Link'],
context
),
),
);
});
} catch(exception){
checkConnection(context);
print(exception);
return CircularProgressIndicator();

}
}
) // getJson()
)

感谢您的回复。我用来解决这个问题的代码是

child: Container(
margin: EdgeInsets.symmetric(vertical: 4, horizontal: 5),
child: FutureBuilder(
future: data, //was getData() so if there are any errors change this and the PROBLEM WILL BE FIXED
builder: (context,
snapshot) { // getting items = snapshot.data[0]['Title']
try{
int connection_check;
if(snapshot.data.length == null || snapshot.data.length == "")
connection_check = 0;
else
connection_check = 20;
return ListView.separated(
separatorBuilder:(context, index) => Divider(
color: Colors.white,
) ,
padding: EdgeInsets.symmetric(vertical: 4),

itemCount: connection_check,

最新更新