格式异常错误 Double INVALID IN New-Transaction 类

发布于 2025-01-11 20:15:54 字数 5967 浏览 0 评论 0原文

当输入字段中的供应值加倍并点击按钮添加新交易时,它会给我错误异常发生双倍无效

这个项目的想法是这是一个关于费用用户提供两个值项目名称和价格的项目项目,该项目将添加到列表中并在屏幕上显示为列表一切正常,但只有一个错误或异常

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文