flutter - 错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:类型“double”不是类型转换中“String”类型的子类型
问题描述
我收到此错误,但无法弄清楚原因和位置。我是 Flutter 的新手,我正在使用 hive db 和 provider 创建一个资金管理应用程序。这是我的添加事务页面,未处理的异常:类型“双”不是类型转换中“字符串”类型的子类型
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"),
)
],
),
),
);
}
}
这是我的 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);
}
}
解决方案
推荐阅读
- vector - 如何从包含 Julia 范围的向量中获取值
- filter - 从 Makefile 变量中提取模式和单词
- c++ - 继承中的函数重载歧义,其中函数具有相同数量的参数
- r - 如何绘制逻辑回归
- java - 使用java从字符串中提取html部分(包括纯文本和html部分)
- string - 将 int 值附加到字符串
- graphviz - Set pre-defined node styles?
- python - 将代码从 Python Turtle 转换为 Tkinter
- typescript - 打字稿 - 在没有参数的函数上需要泛型类型
- xamarin - Xamarin.Android 中的(支持的体系结构)有什么区别