Flutter 向类传递快照数据时出错
我试图将Flutter 向类传递快照数据时出错,flutter,dart,Flutter,Dart,我试图将Snapshot数据传递给一个新类,但出现了一个错误(见下文)。我试图添加快照[index],但又出现了错误,但我不知道为什么。 我正在使用 代码是: Container( height: 171, child: StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection('offers').snaps
Snapshot
数据传递给一个新类,但出现了一个错误(见下文)。我试图添加快照[index],但又出现了错误,但我不知道为什么。
我正在使用
代码是:
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,);
}
},
),
)
容器(
身高:171,
孩子:StreamBuilder(
流:Firestore.instance.collection('offers').snapshots(),
生成器:(BuildContext上下文,
异步快照(快照){
if(snapshot.hasError)返回新文本(“${snapshot.error}”);
交换机(快照.连接状态){
案例连接状态。正在等待:
返回新中心(子项:new CircularProgressIndicator());
违约:
返回新的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 SingleItem扩展了无状态小部件{
最后加倍;
SingleItem({this.doucment});
@凌驾
小部件构建(构建上下文){
返回方向性(
textDirection:textDirection.rtl,
子:容器(
宽度:251,
边缘:边缘组。对称(垂直:5.0,水平:15.0),
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径。圆形(15.0),
boxShadow:[
BoxShadow(颜色:Colors.grey[300],模糊半径:3.0),
],
),
子:堆栈(
儿童:[
定位(
底数:20,
右:120,,
子:容器(
子:Image.asset(
“assets/camera.png”,
宽度:121,
),
),
),
容器(
填充:所有边缘设置(15.0),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
“${doucment.data['name']}”,
样式:TextStyle(fontWeight:fontWeight.bold),
),
正文(
"\$ 39.99",
样式:TextStyle(fontWeight:fontWeight.bold),
),
垫片(),
正文(
“在02:00:25结束”,
),
],
),
),
],
),
),
);
}
}
错误控制台是
Class 'AsyncSnapshot<QuerySnapshot>' has no instance method '[]'.
Receiver: Instance of 'AsyncSnapshot<QuerySnapshot>'
Tried calling: []("name")
类“AsyncSnapshot”没有实例方法“[]”。
接收器:“AsyncSnapshot”的实例
尝试呼叫:[](“名称”)
通过调用
Firestore.instance.collection('offers').snapshots()
您正在检索列表,而不仅仅是单个文档。所以你想要的是这样的:
Text("${doucment.data.documents[index]['name']}",
style: TextStyle(fontWeight: FontWeight.bold))
当我试图传递索引文档[index]时,出现了一个错误。您试图在单个元素中显示一个列表。出于测试目的,您可以使用0作为索引,但应该决定要显示哪个文档,或者使用ListView显示所有文档。在Streambuilder小部件中,当返回到新的SingleItem(doucment:snapshot[index])时,出现了errorsnapshot。data[index]错误与您尝试执行
snapshot[index]
时出现的错误相同吗?