重构 Dart 中的地图对象列表?
Refactoring a list of map objects in Dart?
如何重构这个地图对象列表以便更好地阅读?
目标是拥有一个列表变量,以紧凑的方式存储几个点的 x 和 y 值。不一定是地图对象列表,我只是在写的时候发现它最合适。
final List<Map<String, double>> _positions = [
{
'x': _offsetCircle + _circleDistanceHorizontal * 1,
'y': _height * 0.120
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 2,
'y': _height * 0.075
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 3,
'y': _height * 0.095
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 4,
'y': _height * 0.070
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 5,
'y': _height * 0.085
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 6,
'y': _height * 0.055
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 7,
'y': _height * 0.060
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 8,
'y': _height * 0.060
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 9,
'y': _height * 0.045
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 10,
'y': _height * 0.025
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 11,
'y': _height * 0.04
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 12,
'y': _height * 0.005
}
];
final items = [0.12, 0.075, 0.095];
List<Map<String, double>> result = List<Map<String, double>>.generate(items.length ,(index) {
return {
'x': _offsetCircle + _circleDistanceHorizontal * (index + 1),
'y':_height * items[index]
};
});
@Axot 的回答很好。我认为您可能还想改进存储的数据结构。如果您要存储坐标,也许只需创建一个点 class.
class Point {
final double x;
final double y;
Point(this.x, this.y);
}
double _offsetCircle = 1.0, _circleDistanceHorizontal = 1.0;
double _height = 1.0;
final factors = [0.120, 0.075, 0.095];
final list = Iterable<int>.generate(factors.length)
.map((i) => Point(_offsetCircle + _circleDistanceHorizontal * (i + 1),
_height * factors[i]))
.toList();
如何重构这个地图对象列表以便更好地阅读?
目标是拥有一个列表变量,以紧凑的方式存储几个点的 x 和 y 值。不一定是地图对象列表,我只是在写的时候发现它最合适。
final List<Map<String, double>> _positions = [
{
'x': _offsetCircle + _circleDistanceHorizontal * 1,
'y': _height * 0.120
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 2,
'y': _height * 0.075
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 3,
'y': _height * 0.095
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 4,
'y': _height * 0.070
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 5,
'y': _height * 0.085
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 6,
'y': _height * 0.055
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 7,
'y': _height * 0.060
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 8,
'y': _height * 0.060
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 9,
'y': _height * 0.045
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 10,
'y': _height * 0.025
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 11,
'y': _height * 0.04
},
{
'x': _offsetCircle + _circleDistanceHorizontal * 12,
'y': _height * 0.005
}
];
final items = [0.12, 0.075, 0.095];
List<Map<String, double>> result = List<Map<String, double>>.generate(items.length ,(index) {
return {
'x': _offsetCircle + _circleDistanceHorizontal * (index + 1),
'y':_height * items[index]
};
});
@Axot 的回答很好。我认为您可能还想改进存储的数据结构。如果您要存储坐标,也许只需创建一个点 class.
class Point {
final double x;
final double y;
Point(this.x, this.y);
}
double _offsetCircle = 1.0, _circleDistanceHorizontal = 1.0;
double _height = 1.0;
final factors = [0.120, 0.075, 0.095];
final list = Iterable<int>.generate(factors.length)
.map((i) => Point(_offsetCircle + _circleDistanceHorizontal * (i + 1),
_height * factors[i]))
.toList();