【发布时间】:2020-09-29 03:47:06
【问题描述】:
我是 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 中做到这一点。它只是让一切都变得非常干净。
感谢您的帮助!!
【问题讨论】:
-
就是这样。以为我试过了,但我想没有。谢谢!
标签: class flutter variables dart class-variables