Flutter / Dart类实例和类变量

我是dart的新手,在将数据存储在类中时遇到问题。我知道如何创建实例,但随后我想将每个实例存储到可以轻松访问的地图中。这是我的下面的代码...

class myCar{
  var myMapOfCars = new Map(); // I wanted this to be a class level variable that I can continuously add or remove from. 

  String carName; // Instance

  myCar({this.carName});

  addInfoToMap() {
    // Some logic to increment the index or create the index the 

    myMapOfCars[theKey]  = this.carName; // This should be the class variable.

  }
}

每次调用“ addInfoToMap”时,“ myMapOfCars”实例都会重新初始化并再次为空。我想添加/添加到地图中,以便在那里可以有尽可能多的汽车。我也对其他解决方案持开放态度,我来自Swift,而且我知道您可以在Swift中做到这一点。这只会使一切变得真正干净。

感谢您的帮助!

iCMS 回答:Flutter / Dart类实例和类变量

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2167194.html

大家都在问