Firebase 为什么我的void函数不更新我的int变量?
我有3个int变量,我想在initstate期间通过void函数更新它。我尝试打印它们,并且值是正确的,但是当我尝试在容器中显示它们时,它仍然显示0Firebase 为什么我的void函数不更新我的int变量?,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我有3个int变量,我想在initstate期间通过void函数更新它。我尝试打印它们,并且值是正确的,但是当我尝试在容器中显示它们时,它仍然显示0 int equipmentCount1 = 0; int equipmentCount2 = 0; int equipmentCount3 = 0; @override void initState() { getEquipmentCount('Hammer', equipmentCount1); getEquipmentC
int equipmentCount1 = 0;
int equipmentCount2 = 0;
int equipmentCount3 = 0;
@override
void initState() {
getEquipmentCount('Hammer', equipmentCount1);
getEquipmentCount('Spanner', equipmentCount2);
getEquipmentCount('Screwdriver', equipmentCount3);
super.initState();
}
void getEquipmentCount(String type, int counter) async {
await Firestore.instance
.collection('Notes')
.document('CarNotes')
.collection('PM Overview')
.document(type)
.collection(type)
.getDocuments()
.then((QuerySnapshot snapshot) {
setState(() {
return counter = snapshot.documents.length;
});
});
print(counter);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(equipmentCount1),
Text(equipmentCount2),
Text(equipmentCount3),
int设备计数1=0;
int equipmentCount2=0;
int equipmentCount3=0;
@凌驾
void initState(){
getEquipmentCount('Hammer',equipmentCount1);
getEquipmentCount(“扳手”,equipmentCount2);
getEquipmentCount(“螺丝刀”,equipmentCount3);
super.initState();
}
void getEquipmentCount(字符串类型,int计数器)异步{
等待Firestore.instance
.collection('注释')
.文件(“卡诺特斯”)
.collection(“PM概述”)
.文件(类型)
.收集(类型)
.getDocuments()
.然后((QuerySnapshot快照){
设置状态(){
返回计数器=snapshot.documents.length;
});
});
打印(计数器);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
mainAxisAlignment:mainAxisAlignment.space,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(设备计数1),
文本(设备计数2),
文本(设备计数3),
您应该传入回调函数,以便更新状态。
下面是一个例子:
@override
void initState() {
getEquipmentCount('Hammer', (int count) => setState(() {
equipmentCount1 = count;
}));
// same for the others
super.initState();
}
void getEquipmentCount(String type, ValueChanged<int> onCountChanged) {
Firestore.instance
.collection('Notes')
.document('CarNotes')
.collection('PM Overview')
.document(type)
.collection(type)
.getDocuments()
.then((QuerySnapshot snapshot) {
onCountChanged(snapshot.documents.length);
});
});
print(counter);
}
@覆盖
void initState(){
getEquipmentCount('Hammer',(int count)=>setState((){
设备计数1=计数;
}));
//其他的也一样
super.initState();
}
void getEquipmentCount(字符串类型,值更改为onCountChanged){
Firestore.instance
.collection('注释')
.文件(“卡诺特斯”)
.collection(“PM概述”)
.文件(类型)
.收集(类型)
.getDocuments()
.然后((QuerySnapshot快照){
onCountChanged(快照、文档、长度);
});
});
打印(计数器);
}
另外,
wait
是不必要的,因为您正在Future
上使用then
,并且setState
中的函数不需要返回任何内容。您应该传入回调函数,以便更新状态。
下面是一个例子:
@override
void initState() {
getEquipmentCount('Hammer', (int count) => setState(() {
equipmentCount1 = count;
}));
// same for the others
super.initState();
}
void getEquipmentCount(String type, ValueChanged<int> onCountChanged) {
Firestore.instance
.collection('Notes')
.document('CarNotes')
.collection('PM Overview')
.document(type)
.collection(type)
.getDocuments()
.then((QuerySnapshot snapshot) {
onCountChanged(snapshot.documents.length);
});
});
print(counter);
}
@覆盖
void initState(){
getEquipmentCount('Hammer',(int count)=>setState((){
设备计数1=计数;
}));
//其他的也一样
super.initState();
}
void getEquipmentCount(字符串类型,值更改为onCountChanged){
Firestore.instance
.collection('注释')
.文件(“卡诺特斯”)
.collection(“PM概述”)
.文件(类型)
.收集(类型)
.getDocuments()
.然后((QuerySnapshot快照){
onCountChanged(快照、文档、长度);
});
});
打印(计数器);
}
另外,
wait
是不必要的,因为您正在Future
上使用then
,并且setState
中的函数不需要返回任何内容。值仍然是0,因为在您的方法中,您只更改方法中的计数器
变量,而不是实例变量>equipmentCount3
。您可以创建一个列表来添加所有3个值,然后在生成方法中使用该列表:
int equipmentCount1 = 0;
int equipmentCount2 = 0;
int equipmentCount3 = 0;
List<int> listOfEquipments = List();
void getEquipmentCount(String type, int counter) async {
await Firestore.instance
.collection('Notes')
.document('CarNotes')
.collection('PM Overview')
.document(type)
.collection(type)
.getDocuments()
.then((QuerySnapshot snapshot) {
setState(() {
listOfEquipments.add(snapshot.documents.length);
});
});
}
int设备计数1=0;
int equipmentCount2=0;
int equipmentCount3=0;
设备列表=列表();
void getEquipmentCount(字符串类型,int计数器)异步{
等待Firestore.instance
.collection('注释')
.文件(“卡诺特斯”)
.collection(“PM概述”)
.文件(类型)
.收集(类型)
.getDocuments()
.然后((QuerySnapshot快照){
设置状态(){
设备列表。添加(快照。文档。长度);
});
});
}
要将列表
添加到生成
方法,请检查以下内容:
该值仍然为0,因为在方法中,您只更改方法中的计数器
变量,而不是实例变量equipmentCount3
。您可以创建一个列表来添加所有3个值,然后在build
方法中使用该列表:
int equipmentCount1 = 0;
int equipmentCount2 = 0;
int equipmentCount3 = 0;
List<int> listOfEquipments = List();
void getEquipmentCount(String type, int counter) async {
await Firestore.instance
.collection('Notes')
.document('CarNotes')
.collection('PM Overview')
.document(type)
.collection(type)
.getDocuments()
.then((QuerySnapshot snapshot) {
setState(() {
listOfEquipments.add(snapshot.documents.length);
});
});
}
int设备计数1=0;
int equipmentCount2=0;
int equipmentCount3=0;
设备列表=列表();
void getEquipmentCount(字符串类型,int计数器)异步{
等待Firestore.instance
.collection('注释')
.文件(“卡诺特斯”)
.collection(“PM概述”)
.文件(类型)
.收集(类型)
.getDocuments()
.然后((QuerySnapshot快照){
设置状态(){
设备列表。添加(快照。文档。长度);
});
});
}
要将列表
添加到生成
方法,请检查以下内容: