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



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导入。Text(total.toString(((给出错误,字符串不是double的子类型。如何解决这个问题?

欢迎使用SOF

你不是在解析,而是在选角!这迫使total返回stringnull

更换

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);
}

相关内容

  • 没有找到相关文章

最新更新