【问题标题】:The argument type 'int' can't be assigned to the parameter type 'double'参数类型“int”不能分配给参数类型“double”
【发布时间】:2021-03-23 23:59:13
【问题描述】:

谁能帮助我我正在尝试为电池百分比制作一个圆形百分比指标,但我收到了这个错误,这是我的大学项目工作,第一次写这篇文章我以前从未这样做过,我在编码方面也是新手。

lib/pages/device_dashboard.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,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies flutter-web flutter-animation


    【解决方案1】:

    如果您想向用户显示十进制值,Ketan Ramteke 的回答是正确的。 如果这不是您想要的,您可以保留变量 int(就像您目前拥有的那样),然后将电池双倍水平四舍五入,使其为 int 而不是双倍,您应该不会再收到此错误:

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

    }

    【讨论】:

    • Ketan Ramteke,即使在更改 double _batteryLevel; //&lt;= declare it as a double instead of int //.... Future&lt;void&gt; _getLevel() async { final batteryLevel = await _battery.batteryLevel; setState(() { _batteryLevel = batteryLevel; }); } 之后也无法正常工作,但现在它向我显示了不同的错误 在构建 DeviceDashboard(dirty, state: _DeviceDashboardState#bfe3e): The method '
    • Luis Utrera,现在再次工作,Future&lt;void&gt; _getLevel() async { final batteryLevel = await _battery.batteryLevel; setState(() { _batteryLevel = batteryLevel.round(); }); } 但得到相同的错误 在构建 DeviceDashboard(dirty, state: _DeviceDashboardState#bfe3e) 时引发了以下 NoSuchMethodError:调用了方法 '
    • 即使在我更改后也无法正常工作,double _batteryLevel; Future&lt;void&gt; _getLevel() async { final batteryLevel = await _battery.batteryLevel; setState(() { _batteryLevel = batteryLevel.roundToDouble(); }); }, or _batteryLevel = batteryLevel as double;
    • and `percent: 0.5, //这应该在0.0到1.0之间
    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 2021-12-15
    • 2021-12-11
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多