exception has occurred. lateerror (lateinitializationerror: field '_selecteddate@20462804' has not been initialized.

  1. Summary of my problem
  • Details about my goal
  • Expected Results
  • Actual Results
  • Error Messages (if any)
  1. What have I tried so far -
  2. Here’s the minimum code you would need to reproduce the problem -
    To format your code enter between 3 backticks like this

// ignore_for_file: prefer_const_constructors, use_key_in_widget_constructors, deprecated_member_use, sized_box_for_whitespace, prefer_const_constructors_in_immutables, unnecessary_null_comparison

import ‘package:flutter/material.dart’;
import ‘package:intl/intl.dart’;

class NewTransaction extends StatefulWidget {
final Function addTx;

NewTransaction(this.addTx);

@override
_NewTransactionState createState() => _NewTransactionState();
}

class _NewTransactionState extends State {
final _titleController = TextEditingController();
final amountController = TextEditingController();
late DateTime _selectedDate;

void _submitData() {
final enteredTitle = _titleController.text;
final enteredAmount = double.parse(amountController.text);

if (enteredTitle.isEmpty || enteredAmount <= 0) {
  return;
}

widget.addTx(
  enteredTitle,
  enteredAmount,
);

Navigator.of(context).pop();

}

void _presentDatePicker() {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2022),
lastDate: DateTime.now(),
).then((pickedData) {
if (pickedData == null) {
return;
}
setState(() {
_selectedDate = pickedData;
});
});
}

@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
child: Container(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
TextField(
decoration: InputDecoration(labelText: ‘Title’),
controller: titleController,
onSubmitted: (
) => submitData(),
// onChanged: (val) {
// titleInput = val;
// },
),
TextField(
decoration: InputDecoration(labelText: ‘Amount’),
controller: amountController,
keyboardType: TextInputType.number,
onSubmitted: (
) => _submitData(),
// onChanged: (val) => amountInput = val,
),
Container(
height: 70,
child: Row(
children: [
Text(_selectedDate == null
? ‘No date Chose’
: ‘Picked Date: ${DateFormat.yMd().format(_selectedDate)}’),
FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text(
‘Choose Date’,
style: TextStyle(fontWeight: FontWeight.bold),
),
onPressed: _presentDatePicker,
)
],
),
),
RaisedButton(
child: Text(‘Add Transaction’),
color: Theme.of(context).primaryColor,
textColor: Colors.white,
onPressed: _submitData,
),
],
),
),
);
}
}