我收到此错误,但无法弄清楚原因和位置。 我对 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',
hintStyle: TextStyle(color: Colors.black54),
),
),
SizedBox(height: 30),
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);
}
}