Flutter - setState not updating inner Stateful Widget

后端 未结 5 464
生来不讨喜
生来不讨喜 2021-02-01 02:57

Basically I am trying to make an app whose content will be updated with an async function that takes information from a website, but when I do try to set the new state, it doesn

5条回答
  •  再見小時候
    2021-02-01 03:05

    don't use a future within a future; use different function that will return each future individually like this

     List requestsData;
     List requestsDocumentData;
     var docId;
    
    
    
      @override
      void initState() {
        super.initState();
    
        getRequestDocs();
      }
    
      Future getData() {
        var _auth = FirebaseAuth.instance;
        return _auth.currentUser();
      }
    
      getRequestDocs() {
        getData().then((FirebaseUser user) {
          this.setState(() {
            docId = user.uid;
          });
        });
    
        FireDb()
            .getDocuments("vendorsrequests")
            .then((List documentSnapshots) {
          this.setState(() {
            requestsDocumentData = documentSnapshots;
          });
        });
    
        for (DocumentSnapshot request in requestsDocumentData) {
          this.setState(() {
            requestsData.add(Requests(
                request.documentID,
                request.data['requests'],
                Icons.data_usage,
                request.data['requests'][0],
                "location",
                "payMessage",
                "budget",
                "tokensRequired",
                "date"));
          });
        }
      }
    

    you can create individual functions for

      FireDb().getDocuments("vendorsrequests")
                .then((List documentSnapshots) {
              this.setState(() {
                requestsDocumentData = documentSnapshots;
              });
            });
    

    and

      for (DocumentSnapshot request in requestsDocumentData) {
              this.setState(() {
                requestsData.add(Requests(
                    request.documentID,
                    request.data['requests'],
                    Icons.data_usage,
                    request.data['requests'][0],
                    "location",
                    "payMessage",
                    "budget",
                    "tokensRequired",
                    "date"));
              });
            }
    

    I found that the use of

    this
    

    with setState is must

提交回复
热议问题