格式异常错误 Double INVALID IN New-Transaction 类
当输入字段中的供应值加倍并点击按钮添加新交易时,它会给我错误异常发生双倍无效
这个项目的想法是这是一个关于费用用户提供两个值项目名称和价格的项目项目,该项目将添加到列表中并在屏幕上显示为列表一切正常,但只有一个错误或异常
import 'package:flutter/material.dart';
class NewTransaction extends StatelessWidget {
//NewTransaction({Key? key}) : super(key: key);
//@override
//State<NewTransaction> createState() => _NewTransactionState();
final Function addTx;
final titleController = TextEditingController();
final amountController = TextEditingController();
NewTransaction(this.addTx, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
elevation: 10,
child: Container(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextField(
decoration: const InputDecoration(
labelText: 'Enter Title',
labelStyle: TextStyle(
fontSize: 25,
)),
// onChanged: (value) {
// inputtitle = value;
// },
controller: titleController,
),
TextField(
decoration: const InputDecoration(
labelText: 'Enter Amount',
labelStyle: TextStyle(fontSize: 25)),
//One of this method to save value in a field but we try another method
// onChanged: (value) {
// inputamount = value;
// },
controller: amountController,
),
TextButton(
onPressed: () {
//using lisnter mentod i confirm this way my value is save or not
// print(inputtitle);
// print(inputamount);
// print(titleController.text);
addTx(
titleController.text,
double.parse(amountController.text),
);
},
child: Text('Add Transaction',
style: TextStyle(color: Colors.purple)))
],
),
),
);
}
}
用户事务类
import 'package:flutter/widgets.dart';
import '../modals/transactions.dart';
import 'new_transaction.dart';
import 'transaction_list.dart';
class UserTransactions extends StatefulWidget {
const UserTransactions({Key? key}) : super(key: key);
@override
_UserTransactionsState createState() => _UserTransactionsState();
}
class _UserTransactionsState extends State<UserTransactions> {
final List<TranSaction> _userTransactions = [
TranSaction(
id: 't1',
title: 'New Shoes',
amount: 69.99,
date: DateTime.now(),
),
TranSaction(
id: 't2',
title: 'Weekly Groceries',
amount: 16.53,
date: DateTime.now(),
),
];
void _addNewTransaction(String txTitle, double txAmount) {
final newTx = TranSaction(
title: txTitle,
amount: txAmount,
date: DateTime.now(),
id: DateTime.now().toString(),
);
setState(() {
_userTransactions.add(newTx);
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
NewTransaction(_addNewTransaction),
TransactionList(_userTransactions),
],
);
}
}
import 'package:budget/modals/transactions.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class TransactionList extends StatefulWidget {
const TransactionList(List<TranSaction> userTransactions, {Key? key})
: super(key: key);
@override
_TransactionListState createState() => _TransactionListState();
}
class _TransactionListState extends State<TransactionList> {
final List<TranSaction> _userTransacton = [
TranSaction(
id: 't1', title: 'New Shoes', amount: 69.9, date: DateTime.now()),
TranSaction(
id: 't2',
title: 'Weekly Groceries',
amount: 16.53,
date: DateTime.now())
];
@override
Widget build(BuildContext context) {
return Column(
children: _userTransacton.map(
(tx) {
return Card(
child: Row(
children: [
Container(
margin:
const EdgeInsets.symmetric(vertical: 20, horizontal: 30),
decoration: BoxDecoration(
border: Border.all(color: Colors.purple, width: 2)),
padding: const EdgeInsets.all(10),
child: Text(
//we use here string interpolation to output dollor sign
'\$${tx.amount}',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.purple,
fontSize: 25),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
tx.title,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
SizedBox(
height: 10,
),
Text(
DateFormat('y/m/d').format(tx.date),
style: TextStyle(color: Colors.blueGrey, fontSize: 15),
)
],
)
],
));
},
).toList(),
);
}
}
class TranSaction {
final String id;
final String title;
final double amount;
final DateTime date;
TranSaction(
{required this.id,
required this.title,
required this.amount,
required this.date});
}
When supply value in input field double and hit button ADD NEW transaction it gives me Error Exception occur double Invalid
The idea of this project is This is a project about expenses user supply two value item name and price of the item and this item will add in a list and show on the screen is a list Everything is going well but only one error or exception
import 'package:flutter/material.dart';
class NewTransaction extends StatelessWidget {
//NewTransaction({Key? key}) : super(key: key);
//@override
//State<NewTransaction> createState() => _NewTransactionState();
final Function addTx;
final titleController = TextEditingController();
final amountController = TextEditingController();
NewTransaction(this.addTx, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Card(
elevation: 10,
child: Container(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextField(
decoration: const InputDecoration(
labelText: 'Enter Title',
labelStyle: TextStyle(
fontSize: 25,
)),
// onChanged: (value) {
// inputtitle = value;
// },
controller: titleController,
),
TextField(
decoration: const InputDecoration(
labelText: 'Enter Amount',
labelStyle: TextStyle(fontSize: 25)),
//One of this method to save value in a field but we try another method
// onChanged: (value) {
// inputamount = value;
// },
controller: amountController,
),
TextButton(
onPressed: () {
//using lisnter mentod i confirm this way my value is save or not
// print(inputtitle);
// print(inputamount);
// print(titleController.text);
addTx(
titleController.text,
double.parse(amountController.text),
);
},
child: Text('Add Transaction',
style: TextStyle(color: Colors.purple)))
],
),
),
);
}
}
Format Exception Error: Invalid Double
User Transaction class
import 'package:flutter/widgets.dart';
import '../modals/transactions.dart';
import 'new_transaction.dart';
import 'transaction_list.dart';
class UserTransactions extends StatefulWidget {
const UserTransactions({Key? key}) : super(key: key);
@override
_UserTransactionsState createState() => _UserTransactionsState();
}
class _UserTransactionsState extends State<UserTransactions> {
final List<TranSaction> _userTransactions = [
TranSaction(
id: 't1',
title: 'New Shoes',
amount: 69.99,
date: DateTime.now(),
),
TranSaction(
id: 't2',
title: 'Weekly Groceries',
amount: 16.53,
date: DateTime.now(),
),
];
void _addNewTransaction(String txTitle, double txAmount) {
final newTx = TranSaction(
title: txTitle,
amount: txAmount,
date: DateTime.now(),
id: DateTime.now().toString(),
);
setState(() {
_userTransactions.add(newTx);
});
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
NewTransaction(_addNewTransaction),
TransactionList(_userTransactions),
],
);
}
}
import 'package:budget/modals/transactions.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class TransactionList extends StatefulWidget {
const TransactionList(List<TranSaction> userTransactions, {Key? key})
: super(key: key);
@override
_TransactionListState createState() => _TransactionListState();
}
class _TransactionListState extends State<TransactionList> {
final List<TranSaction> _userTransacton = [
TranSaction(
id: 't1', title: 'New Shoes', amount: 69.9, date: DateTime.now()),
TranSaction(
id: 't2',
title: 'Weekly Groceries',
amount: 16.53,
date: DateTime.now())
];
@override
Widget build(BuildContext context) {
return Column(
children: _userTransacton.map(
(tx) {
return Card(
child: Row(
children: [
Container(
margin:
const EdgeInsets.symmetric(vertical: 20, horizontal: 30),
decoration: BoxDecoration(
border: Border.all(color: Colors.purple, width: 2)),
padding: const EdgeInsets.all(10),
child: Text(
//we use here string interpolation to output dollor sign
'\${tx.amount}',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.purple,
fontSize: 25),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
tx.title,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
SizedBox(
height: 10,
),
Text(
DateFormat('y/m/d').format(tx.date),
style: TextStyle(color: Colors.blueGrey, fontSize: 15),
)
],
)
],
));
},
).toList(),
);
}
}
class TranSaction {
final String id;
final String title;
final double amount;
final DateTime date;
TranSaction(
{required this.id,
required this.title,
required this.amount,
required this.date});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论