提问者:小点点

'类型字符串不是双的子类型'



class Pg2 extends StatelessWidget {
  String amount;
  final a = TextEditingController();
  Pg2(this.amount);
  double get total {
    double amt = (amount as double);
    double p = (a.text as double);
    return (amt + (p / 100) * amt);
  }
  

  @override
  Widget build(BuildContext context) {
    void dialogbox() {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(
                title: Text('You have to pay :'),
                content: Container(
                  child: Text(total.toString()),
                ),
              ));
    }

    return Scaffold(
      body: Column(
        children: [
          Text(amount.toString()),
          TextField(
            decoration:
                InputDecoration(labelText: 'Percent tip you want to give'),
            controller: a,
          ),
          RaisedButton(
            child: Text('Submit'),
            onPressed: dialogbox,
          )
        ],
      ),
    );
  }
}

我使用控制器从pg1导入了数量作为字符串。文本部分(总计. toString())给出字符串不是double的子类型的错误。如何解决这个问题?


共1个答案

匿名用户

欢迎来到SOF

你不是在解析,而是在转换!这会强制总计返回字符串null

替换

double get total {
    double amt = (amount as double);
    double p = (a.text as double);
    return (amt + (p / 100) * amt);
  }

double get total {
    double amt = double.parse(amount);
    double p = double.parse(a.text);
    return (amt + (p / 100) * amt);
  }