0

I am new to flutter and I am trying to build an app which shows the current location by using Google Maps API. There is no error in this code but this error is produced once the programe is run: "Exception has occurred. LateError (LateInitializationError: Field '_selectDate@20370492' has not been initialized.)" Can someone please provide an example code of what's needed to solve for this error?

and I tried DateTime? _selectDate; but it doesn't work also

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class NewTransaction extends StatefulWidget {
  // widget class
  late final Function addTx;

  NewTransaction(this.addTx);

  @override
  State<NewTransaction> createState() => _NewTransactionState();
}

class _NewTransactionState extends State<NewTransaction> {
  // state class
  late String titleInput;

  late String amountInput;

  final _titleController = TextEditingController();

  final _amountController = TextEditingController();

  late DateTime _selectDate ;

  void _submitData() {
    final enteredtitel = _titleController.text;
    final enteredAmount = double.parse(_amountController.text);
    if (enteredtitel.isEmpty || enteredAmount <= 0) {
      return;
    }
    ;
    widget.addTx(
      enteredtitel,
      enteredAmount,
    );

    Navigator.of(context).pop();
  }

  void _presentDatePicker() {
    showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2023),
      lastDate: DateTime.now(),
    ).then((value) {
      if (value == null) {
        return;
      }
      setState(() {
        _selectDate = value;
      });
    });
  }

  @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(),
          ),
          TextField(
            decoration: InputDecoration(
              labelText: 'Amount',
            ),
            controller: _amountController,
            onSubmitted: (_) => _submitData(),
          ),
          Container(
            height: 70,
            child: Row(
              children: [
                Expanded(
                  child: Text('No Date Chosen!'),
                ),
                MaterialButton(
                  child: Text(
                    'Choose Date',
                    style: TextStyle(
                      color: Theme.of(context).primaryColor,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  onPressed: _presentDatePicker,
                )
              ],
            ),
          ),
          MaterialButton(
            onPressed: _submitData,
            child: Text(
              _selectDate == null
                  ? 'Add Transaction'
                  : 'Picked Date: ${DateFormat.yMd().format(_selectDate)}',
            ),
            textColor: Theme.of(context).textTheme.button?.color,
            color: Theme.of(context).primaryColor,
          ),
        ]),
      ),
    );
  }
}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
Ahmed Helmy
  • 79
  • 1
  • 1
  • 6
  • *"I tried `DateTime? _selectDate;` but it doesn't work also"* What happens then? Surely you're not encountering a `LateInitializationError` on a non-`late` variable. It's likely an unrelated problem. – Ryan M Mar 07 '23 at 06:40

1 Answers1

1

change

late DateTime _selectDate ;

to

DateTime? _selectDate ;
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88