【问题标题】:Why doesnt my void function update my int variable?为什么我的 void 函数不更新我的 int 变量?
【发布时间】:2020-05-04 15:57:27
【问题描述】:

我有 3 个 int 变量,我想在 initstate 期间通过 void 函数进行更新。我尝试将它们打印出来,值是正确的,但是当我尝试在容器中显示它们时,它仍然显示 0。

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),


【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您应该传入一个回调函数,以便更新状态。 这是一个例子:

     @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);
      }
    

    另外,await 不是必需的,因为您在 Future 上使用了 then,而 setState 中的函数不需要返回任何内容。

    【讨论】:

      【解决方案2】:

      该值仍为 0,因为在您的方法中,您只更改了方法内部的 counter 变量,而不是实例变量 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);
            });
          });
        }
      

      要将list 添加到build 方法,请检查以下内容:

      https://stackoverflow.com/a/61548797/7015400

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-10
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        相关资源
        最近更新 更多