如何将Flutter滑块double值转换为int

问题描述 投票:0回答:1

我正在为BMI计算器编写代码,并且我正在使用滑块的高度,它会返回一个double类型的long类型。我尝试了_startHeight.toInt(),但没有成功。而且有没有不用分隔就可以显示标签的方法。

这是我的滑块代码。

               Slider(
                  value: _startHeight,
                  min: 1,
                  max: 100,
                  onChanged: (newHeight) {
                    setState(() {
                      _startHeight = newHeight;
                      _startHeight.toInt();
                      print(_startHeight);
                    });
                  },
                ),

谢谢。

flutter dart flutter-layout flutter-test
1个回答
0
投票

如果您想将_startHeight设为int,那么我首先假设您将其定义为int

您正在尝试将double分配给int,然后将该double更改为int。您需要采用其他方法:

onChanged: (double newHeight) {
  setState(() {
    _startHeight = newHeight.toInt();
    print(_startHeight);
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.