问题描述
我收到此错误,但无法弄清楚原因和位置。 我对 Flutter 有点陌生,我正在使用 hive db 和 provider 创建一个资金管理应用程序。这是我的添加交易页面, 未处理的异常:类型“double”不是类型转换中“String”类型的子类型
class _AddTransactionState extends State<AddTransaction> with TickerProviderStateMixin{
String transTitle;
double transAmount=0;
DateTime transDate = DateTime.Now();
Future<void> _selectDate(BuildContext context) async {
final DateTime pickedDate = await showDatePicker(
context: context,initialDate: transDate,firstDate: DateTime(2010),lastDate: DateTime(2050));
if (pickedDate != null && pickedDate != transDate)
setState(() {
transDate = pickedDate;
});
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.black,child: Container(
padding: EdgeInsets.symmetric(vertical: 20,horizontal: 40),height: 550,decoration: Boxdecoration(
color: Color(0xffE8816D),borderRadius: BorderRadius.only(topRight: Radius.circular(20),topLeft: Radius.circular(20)),),child: Column(
children: <Widget>[
Text("New TransactionDetails",style: kdropDownTextStyle.copyWith(decoration: Textdecoration.underline),SizedBox(height: 50,TextField(
onChanged: (value){transTitle = "$value";
print(transTitle);},decoration: kTextFielddecoration.copyWith(
hintText: 'Enter new Transaction',hintStyle: TextStyle(color: Colors.black54),SizedBox(height: 30),TextField(
onChanged: (value){
transAmount = double.parse("$value");
print(transAmount);
},keyboardType: TextInputType.number,decoration: kTextFielddecoration.copyWith(
hintText: 'Enter Amount',Text("${transDate.day}-${transDate.month}-${transDate.year}",style: GoogleFonts.kaushanScript(textStyle: TextStyle(fontSize: 30)),RaisedButton(
onpressed: () => _selectDate(context),child: Text('Select date'),FlatButton(
onpressed: (){
Provider.of<TransData>(context,listen: false).addToList(
Item(
transactionType: widget.transactionType,categoryName: widget.categoryName,amount: transAmount,dateTime: transDate,transName: transTitle,));
Navigator.pop(context);
},child: Text("Add Transaction"),)
],);
}
}
这是我的交易.g.dart 文件
class ItemAdapter extends TypeAdapter<Item> {
@override
final typeId = 1;
@override
Item read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int,dynamic>{
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),};
return Item(
transactionType: fields[0] as String,categoryName: fields[1] as String,transName: fields[2] as String,amount: fields[3] as double,dateTime: fields[4] as DateTime,);
}
@override
void write(BinaryWriter writer,Item obj) {
writer
..writeByte(5)
..writeByte(0)
..write(obj.transactionType)
..writeByte(1)
..write(obj.categoryName)
..writeByte(2)
..write(obj.transName)
..writeByte(3)
..write(obj.amount)
..writeByte(4)
..write(obj.dateTime);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)