gpt4 book ai didi

flutter - 将快照数据传递给类时出错

转载 作者:行者123 更新时间:2023-12-03 04:55:45 24 4
gpt4 key购买 nike

我正在尝试通过 Snapshot数据到一个新的类,但我得到一个错误(见下文)。我尝试添加 Snapshot[index]我又得到了错误,但我不知道为什么。
我正在使用 cloud_firestore .

该代码是:

Container(
height: 171,
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('offers').snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) return new Text('${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Center(child: new CircularProgressIndicator());
default:
return new SingleItem(doucment: snapshot,);
}
},
),
)

SingleItem 类是
class SingleItem extends StatelessWidget {
final doucment;
SingleItem({this.doucment});
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Container(
width: 251,
margin: EdgeInsets.symmetric(vertical: 5.0, horizontal: 15.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15.0),
boxShadow: [
BoxShadow(color: Colors.grey[300], blurRadius: 3.0),
],
),
child: Stack(
children: <Widget>[
Positioned(
bottom: 20,
right: 120,
child: Container(
child: Image.asset(
"assets/camera.png",
width: 121,
),
),
),
Container(
padding: EdgeInsets.all(15.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"${doucment.data['name']}",
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(
"\$ 39.99",
style: TextStyle(fontWeight: FontWeight.bold),
),
Spacer(),
Text(
"Ends in 02:00:25",
),
],
),
),
],
),
),
);
}
}

错误控制台是
Class 'AsyncSnapshot<QuerySnapshot>' has no instance method '[]'.
Receiver: Instance of 'AsyncSnapshot<QuerySnapshot>'
Tried calling: []("name")

最佳答案

通过调用

Firestore.instance.collection('offers').snapshots() 

您正在检索一个列表,而不仅仅是一个文档。所以你想要的是这样的:
Text("${doucment.data.documents[index]['name']}",
style: TextStyle(fontWeight: FontWeight.bold))

关于flutter - 将快照数据传递给类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60625152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com