在饼图颤振中更改值时出现问题
Having issue while changing values in pie chart flutter
我第一次尝试了这段代码,它运行良好。
double a = 2, b = 3, c = 5;
var color;
Map<String, double> dataMap = Map();
List<Color> colorList = [
Colors.red,
Colors.green,
Colors.yellow,
];
void changeGraph() {
dataMap.putIfAbsent("Fat", () => c);
dataMap.putIfAbsent("Protein", () => b);
dataMap.putIfAbsent("Carbs", () => a);
}
void initState() {
super.initState();
changeGraph();
}
和
PieChart(
dataMap: dataMap,
animationDuration: Duration(milliseconds: 800),
chartLegendSpacing: 32.0,
chartRadius: MediaQuery.of(context).size.width / 2.7,
showChartValuesInPercentage: true,
showChartValues: true,
showChartValuesOutside: false,
chartValueBackgroundColor: Colors.grey[200],
colorList: colorList,
showLegends: true,
legendPosition: LegendPosition.right,
decimalPlaces: 1,
showChartValueLabel: true,
initialAngle: 0,
chartValueStyle: defaultChartValueStyle.copyWith(
color: Colors.blueGrey[900].withOpacity(0.9),
),
chartType: ChartType.disc,
)
然后在从用户那里获取值后,我尝试了这种更改图表的方法
setState(() {
a = newA;
b = newB;
c = newC;
});
我也尝试调用 changeGraph() 方法,但图形没有改变,它显示的是它第一次显示的值。
有什么方法可以更改值吗?
您在这里所做的只是更改变量的值,而不是 map.The 映射中的值具有 a = 2 的值而不是对 a 的引用。
意味着当你说 dataMap.putIfAbsent("Carbs", () => a);
时 "Carbs" 的值不是 a 但它实际上是 2 因为这里的值是一个 int 而不是引用。
为了更改地图中 Carbs 的值,您需要直接从地图本身更改它,例如 datamap["Carbs"] = newA
。同样适用于 b 和 c
如果这不起作用请告诉我
我第一次尝试了这段代码,它运行良好。
double a = 2, b = 3, c = 5;
var color;
Map<String, double> dataMap = Map();
List<Color> colorList = [
Colors.red,
Colors.green,
Colors.yellow,
];
void changeGraph() {
dataMap.putIfAbsent("Fat", () => c);
dataMap.putIfAbsent("Protein", () => b);
dataMap.putIfAbsent("Carbs", () => a);
}
void initState() {
super.initState();
changeGraph();
}
和
PieChart(
dataMap: dataMap,
animationDuration: Duration(milliseconds: 800),
chartLegendSpacing: 32.0,
chartRadius: MediaQuery.of(context).size.width / 2.7,
showChartValuesInPercentage: true,
showChartValues: true,
showChartValuesOutside: false,
chartValueBackgroundColor: Colors.grey[200],
colorList: colorList,
showLegends: true,
legendPosition: LegendPosition.right,
decimalPlaces: 1,
showChartValueLabel: true,
initialAngle: 0,
chartValueStyle: defaultChartValueStyle.copyWith(
color: Colors.blueGrey[900].withOpacity(0.9),
),
chartType: ChartType.disc,
)
然后在从用户那里获取值后,我尝试了这种更改图表的方法
setState(() {
a = newA;
b = newB;
c = newC;
});
我也尝试调用 changeGraph() 方法,但图形没有改变,它显示的是它第一次显示的值。 有什么方法可以更改值吗?
您在这里所做的只是更改变量的值,而不是 map.The 映射中的值具有 a = 2 的值而不是对 a 的引用。
意味着当你说 dataMap.putIfAbsent("Carbs", () => a);
时 "Carbs" 的值不是 a 但它实际上是 2 因为这里的值是一个 int 而不是引用。
为了更改地图中 Carbs 的值,您需要直接从地图本身更改它,例如 datamap["Carbs"] = newA
。同样适用于 b 和 c
如果这不起作用请告诉我