Flutter 参数类型';int';can';不能分配给参数类型';双倍';

Flutter 参数类型';int';can';不能分配给参数类型';双倍';,flutter,flutter-layout,flutter-dependencies,flutter-web,flutter-animation,Flutter,Flutter Layout,Flutter Dependencies,Flutter Web,Flutter Animation,有人能帮我吗?我正在尝试为电池百分比做一个循环百分比指示器,但我得到了这个错误,这是我大学项目的工作,这是我第一次写这篇文章,我以前从未做过,而且我对编码也很陌生 lib/pages/device\u仪表板。dart:139:36:错误:无法将参数类型“int”分配给参数类型“double”。 百分比:_batteryLevel, ^ 这是我的密码 import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'pack

有人能帮我吗?我正在尝试为电池百分比做一个循环百分比指示器,但我得到了这个错误,这是我大学项目的工作,这是我第一次写这篇文章,我以前从未做过,而且我对编码也很陌生

lib/pages/device\u仪表板。dart:139:36:错误:无法将参数类型“int”分配给参数类型“double”。 百分比:_batteryLevel, ^

这是我的密码

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'package:battery/battery.dart';
import 'package:flutter/material.dart';

class DeviceDashboard extends StatefulWidget {
  @override
  _DeviceDashboardState createState() => _DeviceDashboardState();
}

class _DeviceDashboardState extends State<DeviceDashboard> {
  Battery _battery = Battery();
  BatteryState _batteryState;

  int _batteryLevel;

  StreamSubscription<BatteryState> _batteryStateSubscription;
 

  @override
  void initState() {
    super.initState();
    _batteryStateSubscription =
        _battery.onBatteryStateChanged.listen((BatteryState state) {
      setState(() {
        _batteryState = state;
      });
    });
  }

  Future<void> _getLevel() async {
    final batteryLevel = await _battery.batteryLevel;
    setState(() {
      _batteryLevel = batteryLevel;
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if (_batteryStateSubscription != null) {
      _batteryStateSubscription.cancel();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Device Dashboard'),
        ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 50,
            ),
            Text(
              '$_batteryState',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Colors.grey[400],
              ),
            ),

            Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                CircularPercentIndicator(
                  radius: 100.0,
                  lineWidth: 7.0,
                  center: MaterialButton(
                    onPressed: _getLevel,
                    textColor: Colors.white,
                    child: Text(
                      '$_batteryLevel %',
                      style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 20),
                    ),
                    padding: EdgeInsets.all(20),
                    shape: CircleBorder(),
                  ),
                  progressColor: Colors.green,
                  percent: 0.5, // after putting _batteryLevel here i m getting that error 
                ),
                Text(
                  'Battery',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

导入'dart:async';
进口“包装:颤振/cupertino.dart”;
导入“包:百分比指示器/百分比指示器.dart”;
进口“包装:电池/电池.dart”;
进口“包装:颤振/材料.省道”;
类DeviceDashboard扩展StatefulWidget{
@凌驾
_DeviceDashboardState createState()=>\u DeviceDashboardState();
}
类_DeviceDashboardState扩展状态{
电池_电池=电池();
BatteryState _BatteryState;
国际电池水平;
StreamSubscription\u batteryStateSubscription;
@凌驾
void initState(){
super.initState();
_BatteryState订阅=
_battery.onBatteryStateChanged.listen((BatteryState状态){
设置状态(){
_电池状态=状态;
});
});
}
Future\u getLevel()异步{
最终电池水平=等待电池水平;
设置状态(){
_batteryLevel=batteryLevel;
});
}
@凌驾
无效处置(){
//TODO:实现dispose
super.dispose();
如果(_batteryStateSubscription!=null){
_batteryStateSubscription.cancel();
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“设备仪表板”),
),
正文:SingleChildScrollView(
子:列(
儿童:[
大小盒子(
身高:50,
),
正文(
“$\u batteryState”,
样式:TextStyle(
尺码:18,
fontWeight:fontWeight.bold,
颜色:颜色。灰色[400],
),
),
纵队(
mainAxisSize:mainAxisSize.min,
儿童:[
循环指示器(
半径:100.0,
线宽:7.0,
中心:物料按钮(
onPressed:_getLevel,
textColor:Colors.white,
子:文本(
“$\u batteryLevel%”,
样式:TextStyle(
颜色:颜色,白色,
fontWeight:fontWeight.bold,
尺寸:20),,
),
填充:边缘设置。全部(20),
形状:CircleBorder(),
),
progressColor:Colors.green,
百分比:0.5,//在这里输入_batteryLevel之后,我得到了那个错误
),
正文(
“电池”,
样式:TextStyle(
尺寸:20,
fontWeight:fontWeight.bold,
颜色:颜色,白色,
),
),
],
),
],
),
),
);
}
}

如果要向用户显示十进制值,Ketan Ramtek的答案是正确的。 如果这不是您想要的,您可以保留变量int(正如您当前所拥有的那样),只需将电池双电平四舍五入,使其为int而不是double,您不应该再收到此错误:

Future<void> _getLevel() async {
final batteryLevel = await _battery.batteryLevel;
setState(() {
  _batteryLevel = batteryLevel.round();
});
Future\u getLevel()异步{
最终电池水平=等待电池水平;
设置状态(){
_batteryLevel=batteryLevel.round();
});

}

Ketan Ramtek,不工作,即使在更换
double\u电池等级后//Luis Uterra,现在又开始工作了,
Future getLevel()异步{final batteryLevel=wait battery.batteryLevel;setState({{u batteryLevel=batteryLevel.round()})}
但是在构建DeviceDashboard(脏,状态:DeviceDashboardState#:方法“即使在我更改后仍不工作,
double\u batteryLevel;Future{U getLevel()异步{final batteryLevel=Wait{U battery.batteryLevel;设置状态((){U batteryLevel=batteryLevel.roundToDouble();})},或{U batteryLevel=batteryLevel为双精度
和'percent:0.5,//该值仅应在0.0到1.0之间