Manan Patel
Manan Patel

Reputation: 9

Too many positional arguments: 0 expected, but 1 found. Try removing the extra arguments

UserTransaction.dart

class UserTransactions extends StatefulWidget {
  @override
  _UserTransactionsState createState() => _UserTransactionsState();
}

class _UserTransactionsState extends State<UserTransactions> {
  final List<Transaction> _userTransactions = [
    Transaction(
      id: "y1",
      title: "My gift",
      amount: 98.5,
      date: DateTime.now(),
    ),
    Transaction(
      id: "y2",
      title: "My Fruits",
      amount: 18.5,
      date: DateTime.now(),
    ),
  ];

  void _addNewTransaction(String txTitle, double txAmount) {
    final newTx = Transaction(
      title: txTitle,
      amount: txAmount,
      id: DateTime.now().toString(),
      date: DateTime.now(),
    );

    setState(() {
      _userTransactions.add(newTx);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        NewTransaction(_addNewTransaction),
        TransactionList(_userTransactions),
      ],
    );
  }
}

new_transaction.dart

import 'package:flutter/material.dart';

class NewTransaction extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final Function addTx = () {};
    final titleController = TextEditingController();
    final amountController = TextEditingController();

    NewTransaction(this.addTx);

    return Card(
      elevation: 5,
      child: Container(
        padding: EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            TextField(
              decoration: InputDecoration(labelText: "Title"),
              // onChanged: (val) {
              //   titleInput = val;
              // },
              controller: titleController,
            ),
            TextField(
              decoration: InputDecoration(labelText: "Amount"),
              // onChanged: (val) => amountInput = val,
              controller: amountController,
            ),
            FlatButton(
              onPressed: () {
                addTx(
                  titleController.text,
                  double.parse(amountController.text),
                );
              },
              child: Text("Add Transaction"),
              textColor: Colors.purple,
            )
          ],
        ),
      ),
    );
  }
}

I got the error about the positional argument. Error: Too many positional arguments: 0 expected, but 1 found. Try removing the extra arguments. I tried to solve this error many times but I did not understand how can I solve this error. So, please help me with that error.

Upvotes: 0

Views: 713

Answers (2)

FadyFouad
FadyFouad

Reputation: 948

You declared this constructor NewTransaction(this.addTx); inside build Widget You should Declare it before any methods

import 'package:flutter/material.dart';

class NewTransaction extends StatelessWidget {

final Function addTx = () {};
final titleController = TextEditingController();
final amountController = TextEditingController();

NewTransaction(this.addTx);

  @override
  Widget build(BuildContext context) {
    //remove this Code outside the build method
    /** final Function addTx = () {};
    final titleController = TextEditingController();
    final amountController = TextEditingController();

    NewTransaction(this.addTx);
**/
    return Card(
            //Your Code Here
      );
    }
  }

Upvotes: 1

Jahidul Islam
Jahidul Islam

Reputation: 12575

You should declare a constructor outside the buildcontext

import 'package:flutter/material.dart';

class NewTransaction extends StatelessWidget {
 final Function addTx = () {};
    final titleController = TextEditingController();
    final amountController = TextEditingController();

    NewTransaction(this.addTx);
  @override
  Widget build(BuildContext context) {
 
    return Card(
      elevation: 5,
      child: Container(
        padding: EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            TextField(
              decoration: InputDecoration(labelText: "Title"),
              // onChanged: (val) {
              //   titleInput = val;
              // },
              controller: titleController,
            ),
            TextField(
              decoration: InputDecoration(labelText: "Amount"),
              // onChanged: (val) => amountInput = val,
              controller: amountController,
            ),
            FlatButton(
              onPressed: () {
                addTx(
                  titleController.text,
                  double.parse(amountController.text),
                );
              },
              child: Text("Add Transaction"),
              textColor: Colors.purple,
            )
          ],
        ),
      ),
    );
  }
}

Upvotes: 1

Related Questions