该方法[]不能无条件调用,因为接收器可以为null。我正在尝试读取数据中的数据集
这是我的产品提供商的一部分,我正在呼吁主页将图像和文本返回屏幕。在element.data()的产品函数内部,我试图用element.get('field value')替换它。错误消失了,但没有写在屏幕上。我确定问题是无效的,但是添加了吗?或者 ! toelement.data?()或element.data!()什么都不解决。我不知道其他要提供的友好的见解,
List<Product> homeFeature = [];
late Product homeFeaturedata;
Future<void> gethomefeaturedata() async {
List<Product> newlist = [];
QuerySnapshot homeFeaturesnapshot =
await FirebaseFirestore.instance.collection("homefeature").get();
homeFeaturesnapshot.docs.forEach((element) {
var homeFeaturedata = Product(
image: element.data()["image"],
name: element.data()["name"],
price: element.data()["image"]);
newlist.add(homeFeaturedata);
});
homeFeature = newlist;
}
List<Product> get gethomefeaturelist {
return homeFeature;
}
[enter image description here][1]
[1]: https://i.sstatic.net/wVveO.png
这是我将从querysnapshot调用数据的一部分
Row(
children: homeFeaturedProduct.map((e) {
return Expanded(
child: Row(children: [
Expanded(
child: GestureDetector(
onTap: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => DetailView(
image: e.image, price: e.price, name: e.name)),
);
},
child: SingleProduct(
image: e.image, price: e.price, name: e.name),
),
),
GestureDetector(
onTap: (() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => DetailView(
image: e.image, price: e.price, name: e.name)),
);
}),
child: SingleProduct(
image: e.image,
price: e.price,
name: e.name,
)),
]),
);
}).toList(),
)
this is part of my product provider that i am calling on the homepage to return images and text to the screen. inside the product funtion at the element.data() i have tried to replace it with element.get('field value')the error disappears but nothing is written to the screen. im certain the issue is with null safety but adding ? or ! toelement.data?() or element.data!() solves nothing. i dont know whta else to do kindly offer any insight
List<Product> homeFeature = [];
late Product homeFeaturedata;
Future<void> gethomefeaturedata() async {
List<Product> newlist = [];
QuerySnapshot homeFeaturesnapshot =
await FirebaseFirestore.instance.collection("homefeature").get();
homeFeaturesnapshot.docs.forEach((element) {
var homeFeaturedata = Product(
image: element.data()["image"],
name: element.data()["name"],
price: element.data()["image"]);
newlist.add(homeFeaturedata);
});
homeFeature = newlist;
}
List<Product> get gethomefeaturelist {
return homeFeature;
}
[enter image description here][1]
[1]: https://i.sstatic.net/wVveO.png
This is part of the homepage where i will be calling the data from the querysnapshot
Row(
children: homeFeaturedProduct.map((e) {
return Expanded(
child: Row(children: [
Expanded(
child: GestureDetector(
onTap: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => DetailView(
image: e.image, price: e.price, name: e.name)),
);
},
child: SingleProduct(
image: e.image, price: e.price, name: e.name),
),
),
GestureDetector(
onTap: (() {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => DetailView(
image: e.image, price: e.price, name: e.name)),
);
}),
child: SingleProduct(
image: e.image,
price: e.price,
name: e.name,
)),
]),
);
}).toList(),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
爆炸操作员(感叹号)应在方法调用之后出现。因此,您的情况,
data!()
实际上应该是data()!
。除此之外,我建议,如果您在外壳中多次使用相同的方法,最好将其取出变量。像这样:
作为旁注,您应该注意命名约定。 DART建议使用
Camelcase
进行变量(并且您的IDE也应该提醒您此事)。例如,homefeaturedata
应为homefeaturedata
,gethomefeaturedata
应为gethomefeaturedata
。最后,我认为您不需要
晚产品homefeaturedata;
语句,因为它在范围内未使用。只需将var
替换为final
。The bang operator (the exclamation mark) should come after the method call. So your case,
data!()
should actually bedata()!
.Aside from that, I would suggest that if you use the same method more than once in an enclosure, it's better to take it out into a variable. Like so:
As a side note, you should pay attention to your naming convention. Dart recommends using
camelCase
for variables (and your IDE should have alerted you to this as well). For example,homeFeaturedata
should behomeFeatureData
, andgethomefeaturedata
should begetHomeFeatureData
.Lastly, I don't think you need the
late Product homeFeaturedata;
statement, as it is not used outside of theforeach
scope. Just replace thevar
withfinal
.