簡體   English   中英

Flutter 在for循環中存儲所有數據后列出,當循環外用最后一條記錄替換所有存儲的記錄。 為什么?

[英]Flutter List after storing all data in a for-loop, when out of loop replace all stored records with the last record. Why's that?

我想將數據從 json 傳遞到列表。 我將帶有數據的列表加載到 for 循環中,它按預期工作,將 6 條記錄加載到我的列表中。 但是當我驗證 for 循環何時完成時,我的列表只保留所有 6 條記錄中的最后一個數據。 為什么? 這是我的代碼:

    class LoadData {
          loadSalesData() async {
              String optiune = 'gen';
            String numeServer = Global.server;
            String tabel = Global.tabel;
            SalesData sd = SalesData("", 0.00);
            List<SalesData> sData = [];
            Loader kk = Loader();
            kk
                .searchOnServer(tabel: tabel, numeServer: numeServer, optiune: optiune, dataInceput: "2022-06-30", dataSfarsit: "2022-07-23")
                .then((rezultat) async {
              try {
                rezultat = "[" + rezultat + "]";
               
                var _json = jsonDecode(rezultat);
                Global.dataChart = [];
        
                for (int i = 0; i < _json.length; i++) {
                  // window.alert(_json[i]['partener'] + " Lenght:" + _json.length.toString());
                  sd.partener = _json[i]['partener'];
                  sd.sales = double.parse(_json[i]['Sales']);
                 
                  Global.dataChart.add(sd);
                 //This show the list correctly with all data from server
                  window.alert("Into for-loop $i:" + Global.dataChart[i].partener);
                }
        //This shows only the last record no matter the List index (i.e. [0],[1]...etc
                window.alert("Outside for-loop: " + Global.dataChart[0].partener);
        //This shows the same value as the previous
                window.alert("Outside for-loop: " + Global.dataChart[1].partener);
              } catch (e) {
                print(e);
              }
            });
          }
        }
        
        //Global.dataChart is defined in a separate class as static List<SalesData> dataChart = [];
        //and class SalesData is..
        
        class SalesData {
          String partener;
          double sales;
        
          SalesData(this.partener, this.sales);
        
        }

這是因為您正在編輯SalesData sd的同一個實例,並且 dart 對非原始類型的對象使用引用調用

SalesData sd = SalesData("", 0.00); 而是在循環中。

像這樣:

            for (int i = 0; i < _json.length; i++) {
                  SalesData sd = SalesData("", 0.00);
                  // window.alert(_json[i]['partener'] + " Lenght:" + _json.length.toString());
                  sd.partener = _json[i]['partener'];
                  sd.sales = double.parse(_json[i]['Sales']);
                 
                  Global.dataChart.add(sd);
                 //This show the list correctly with all data from server
                  window.alert("Into for-loop $i:" + Global.dataChart[i].partener);
                }

為了輕松重現和更好地理解,試試這個:

void main() {
  final x = X("random");
  final l = [];
  for(int i =0; i < 3; i ++){
    x.a = i.toString();
    l.add(x);
  }
  l.forEach((e) => print(e.a));
}

class X{
  String a;
  X(this.a);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM