I’m a designer dipping my toes on programming by following a tutorial. It’s supposed to be an app that saves and shows transactions made within the last week. But I’m getting this error message whenever I try to add a transaction on the app. It throws me this error:
════════ Exception caught by gesture ═══════════════════════════════════════════
Closure call with mismatched arguments: function ‘_MyHomePageState._addNewTransaction’
Receiver: Closure: (String, double) => void from Function ‘_addNewTransaction@17043806’:.
Tried calling: _MyHomePageState._addNewTransaction("Wednesday", 555.0, Instance of ‘DateTime’)
Found: _MyHomePageState._addNewTransaction(String, double) => void
════════════════════════════════════════════════════════════════════════════════
In simple terms, how could I fix this?
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class NewTransaction extends StatefulWidget {
final Function addTx;
NewTransaction(this.addTx);
@override
State<NewTransaction> createState() => _NewTransactionState();
}
class _NewTransactionState extends State<NewTransaction> {
final _titleController = TextEditingController();
final _amountController = TextEditingController();
DateTime _selectedDate;
void _submitData() {
if (_amountController.text.isEmpty) {
return;
}
final enteredTitle = _titleController.text;
final enteredAmount = double.parse(_amountController.text);
if (enteredTitle.isEmpty || enteredAmount <= 0 || _selectedDate == null) {
return;
}
widget.addTx(
enteredTitle,
enteredAmount,
_selectedDate,
);
Navigator.of(context).pop();
}
void _presentDatePicker() {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2023),
lastDate: DateTime.now(),
).then((pickedDate) {
if (pickedDate == null) {
return;
}
setState(() {
_selectedDate = pickedDate;
});
});
}
@override
Widget build(BuildContext context) {
return Card(
child: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextField(
decoration: InputDecoration(labelText: 'Title'),
controller: _titleController,
onSubmitted: (_) => _submitData(),
),
TextField(
decoration: InputDecoration(labelText: 'Amount'),
controller: _amountController,
keyboardType: TextInputType.number,
onSubmitted: (_) => _submitData(),
),
Row(
children: [
Flexible(
fit: FlexFit.tight,
child: Text(
_selectedDate == null
? 'No Date Chosen'
: 'Picked Date: ${DateFormat.MEd().format(_selectedDate)}',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
TextButton(
child: Text(
'Choose Date',
style: TextStyle(fontWeight: FontWeight.bold),
),
onPressed: _presentDatePicker)
],
),
SizedBox(
height: 60,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.secondary,
foregroundColor:
Theme.of(context).textTheme.labelLarge.color,
textStyle: TextStyle(fontWeight: FontWeight.w600)),
onPressed: _submitData,
child: Text('Add transaction'),
),
)
],
),
),
);
}
}
What I’ve tried and sort of "worked"
It seems to run when I delete _selectedDate
from the widget.addTx but without it, it won’t save the transaction with the correct date.
What is supposed to happen
The problem occurs when I tap the Add Transaction button, it’s supposed to save the transaction and show it on a list. But I can’t seem to actually add a transaction since I’ve implemented the selectedDate parameter.
>Solution :
The error is due to a mismatch between how the addTx
function is called and how the function passed in is actually defined. The number and/or types of arguments do not match.
Since you have not provided an example of how NewTransaction()
is used, the best way to get to the bottom is to declare addTx
more precisely.
Instead of
final Function addTx;
Declare it as
final void Function(String, double, DateTime) addTx;
Then you should see a static type-checking error at any location that passes in the incorrect function type.