因此,在futurebuilder
和streambuilder
正常工作之前,但是我listbuilder
在streambuilder
中工作,所以我决定进行更改,但现在它不工作了,我在futurebuilder
和streambuilder
中放置了打印件,但是它们没有被打印,什么也没发生。.
我没有收到任何错误。我100%确信查询是正确的。
这是整个代码:
import 'package:flutter/material.dart';
import 'package:rezervisanje/globals.dart' as globals;
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
class ReserveTable extends StatefulWidget {
@override
_ReserveTableState createState() => _ReserveTableState();
}
class _ReserveTableState extends State<ReserveTable>
{
var sum;
var step = 0;
var hours = 0;
var minutes = 0;
DateTime times;
final List<LoadList> terminlist = new List();
final List<LoadList> items = new List();
final formKey = GlobalKey<FormState>();
Future<int> firebaseQuery;
@override
void initState() {
super.initState();
firebaseQuery = Firestore.instance
.collection('salons')
.document(globals.salonkey)
.get()
.then((document) {
setState(() {
globals.shiftstart = document.data['firstshiftstart'];
globals.shiftend = document.data['firstshiftend'];
});
});
}
@override
Widget build(BuildContext context) {
if (globals.day == null) {
globals.day = DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day);
}
times = globals.day.add(new Duration(days: -1));
new FutureBuilder(
future: firebaseQuery,
builder: (context, snapshot) {
if (snapshot.hasError) {
print("snapErr");//not getting printed
return new Text("${snapshot.error}");
} else
items.clear();
print("SHIFTEND " + globals.shiftend.toString());//not getting printed
},
);
new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('termins')
.where('time', isLessThanOrEqualTo: globals.day)
.where('time', isGreaterThanOrEqualTo: times)
.where('employeeID', isEqualTo: globals.employeeID)
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
print("2");//not getting printed
if (!snapshot.hasData)
return const Text('Connecting...');
else {
print('3');//not getting printed
sum = snapshot.data.documents.length;
for (int i = 0; i < sum; i++) {
DocumentSnapshot items = snapshot.data.documents[i];
DateTime date = items['time'];
terminlist.add(new LoadList(date.hour, date.minute));
}
}
});
int div =
((globals.shiftend - globals.shiftstart) * 60 / globals.requiredtime)
.round();
hours = globals.shiftstart;
for (var i = 0; i < div; i++) {
if (minutes == 60) {
minutes = 0;
hours = hours + 1;
}
items.add(new LoadList(hours, minutes));
minutes += globals.requiredtime;
}
return new Scaffold(
appBar: new AppBar(
title:
new Text(globals.employee == "" ? "Reserve" : globals.employee),
backgroundColor: Colors.deepOrange,
),
drawer: new Drawer(
child: new ListView(children: <Widget>[
new ListTile(
title: new Text("Close"),
trailing: new Icon(Icons.arrow_upward),
onTap: () {
Navigator.of(context).pop();
})
])),
body: Card(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Form(
key: formKey,
child: new ListView.builder(
scrollDirection: Axis.vertical,
itemCount: items.length,
itemBuilder: (context, index) {
print(sum);//only this is getting printed,its null
})),
)));
}
}
class LoadList {
final hours;
final minutes;
LoadList(this.hours, this.minutes);
}
我一直在搜索问题几个小时,该怎么改变? 谢谢!
答案 0 :(得分:0)
这是我一起使用它们的方式:
class _PostListState extends State<PostList> {
@override
Widget build(BuildContext context) {
return StreamBuilder<PostParams>(
stream: PostBloc().param,
builder: (BuildContext context, AsyncSnapshot<PostParams> params) {
if (params.hasError) {
return new Center(child: Text(Api.StreamError));
} else {
return new FutureBuilder(
future: Post.get(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new Center(child: new CircularProgressIndicator());
default:
if (snapshot.hasError)
return new Center(child: Text(Api.FutureError));
else {
return new ListBuilder(snapshot.data);
}
}
},
);
}
},
);
}
通过这种方式 StreamBuilder 负责处理来自UI的参数, FutureBuilder 调用API并在结果返回时采取行动。
如果您在Stream中没有更改某些状态,则不需要StreamBuilder。