我正在开发一个字典app..And,我在点击搜索图标时得到了这个错误。
_CastError (Null check operator used on a null value)这是我代码的一部分
Container(
child: ListTile(
title: Text(data.word!),
subtitle: Text(
data.phonetics![index].text!),
trailing: IconButton(
onPressed: () {
final path = data
.phonetics![index]
.audio;
playAudio("https:$path");
},
icon: const Icon(
Icons.audiotrack)),
),
),这就是异常所指向的代码
data.phonetics![index].text!我正在使用颤振2.5.3帮助我提前解决这个error..Thanks
发布于 2022-09-25 14:01:17
尝试在文本大小写上接受null,并在使用!之前检查null
Container(
child: ListTile(
title: Text("${data.word}"),
subtitle: Text("${data.phonetics?[index].text}"),
trailing: IconButton(
onPressed: () {
final path = data.phonetics?[index].audio;
if (path != null) {
playAudio("https:$path");
}
},
icon: const Icon(Icons.audiotrack)),
),
),发布于 2022-09-25 16:19:40
错误解释: Bang操作符(!)这意味着在颤振中,当使用这个运算符时,您完全确定变量在任何情况下都不会是空的。
正如您所写的data.phonetics![index].text!,这是产生错误的地方。现在错误是说phonetics或text都是null。
有两种方法可以和平解决这个问题-
E 215操作符e 116??E 217这样的H 218G 219${data.phonetics?[index].text ?? ''}https://stackoverflow.com/questions/73845063
复制相似问题