我想把Qureysnapshot数据从一个widget传递到另一个widget。我从firebase获取数据,我想把这些数据传递给各个小组件。如何将数据从一个屏幕传递到另一个屏幕,这是我的代码看起来像。
class Dashboard extends StatefulWidget {
@override
_DashboardState createState() => _DashboardState();
}
class _DashboardState extends State<Dashboard> {
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection('schedules').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
snapshotData = snapshot.data;
return Container(
child: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
if (snapshot.data.documents[index]['uid']
.contains(userData.uid)) {
return GestureDetector(
onTap: () {
Route route = MaterialPageRoute(
builder: (context) => ViewSchedule(//here i want pass));
Navigator.push(context, route);
},
child: Container(
height: 180,
width: double.infinity,
),
);
} else {
return Container();
}
})),
],
));
} else {
return Column();
}
});
}
}
在你的ViewSchedule类中,添加一个构造函数。
final QuerySnapshot querySnapshot;
ViewSchedule({@required this.querySnapshot})
然后在你的MaterialPageRoute中添加以下更改
MaterialPageRoute(
builder: (context) => ViewSchedule(querySnapshot:snapshot.data));// pass querySnapshot here
现在,如果你的 ViewSchedule
小部件是无状态的,你可以直接用以下方法访问它。this.querySnapshot
在您的小组件中打印,并使用
print("Printing querysnapshot: ${this.querySnapshot}");
如果它是一个有状态的小部件,那么您可以通过使用 widget.querySnapshot
在您的有状态小组件中。
print("Printing querysnapshot: ${widget.querySnapshot}");