How to check for a nullable value in DateTime initialized variable in Flutter
2,481
Solution 1
If _selectedDate
property does not have a default value, make it nullable:
class _NewTransactionState extends State<NewTransaction> {
...
DateTime? _selectedDate;
...
}
Solution 2
The _selectedDate is nullable. So mark the variable as,
DateTime? _selectedDate;
And before using it have to check for null first,
Text(_selectedDate == null
? 'No Date Chosen!'
: DateFormat.yMd().format(_selectedDate!),
),
Helpful Links->
Author by
Admin
Updated on December 30, 2022Comments
-
Admin over 1 year
There are two main issues :
- The initialization of DateTime variable is not allowed
- The ternary check for a dateTime variable once randomly initialized by Date.now() still cant check for null value.
The error in DateTime variable assigning
After initialization of DateTime variable with Date.now()
import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; class NewTransaction extends StatefulWidget { final Function transactionAddHandler; NewTransaction(this.transactionAddHandler); @override _NewTransactionState createState() => _NewTransactionState(); } class _NewTransactionState extends State<NewTransaction> { final _titleController = TextEditingController(); final _amountController = TextEditingController(); DateTime _selectedDate; void _submitData() { final enteredTitle = _titleController.text; final enteredAmount = double.parse(_amountController.text); if (enteredAmount <= 0 || enteredTitle.isEmpty) { return; } widget.transactionAddHandler(enteredTitle, enteredAmount); Navigator.of(context).pop(); } void _presentDatePicker() { showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(2021), lastDate: DateTime.now(), ).then((pickedDate) { if (pickedDate == null) { return; } setState(() { _selectedDate = pickedDate; }); }); } @override Widget build(BuildContext context) { return Card( elevation: 5, child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.end, children: <Widget>[ TextField( decoration: InputDecoration(labelText: 'Title'), controller: _titleController, onSubmitted: (_) => _submitData(), ), TextField( decoration: InputDecoration(labelText: 'Amount'), keyboardType: TextInputType.number, controller: _amountController, onSubmitted: (_) => _submitData(), ), Container( height: 70, child: Row( children: <Widget>[ Text( _selectedDate == null ? 'No Date Chosen!' : DateFormat.yMd().format(_selectedDate), ), FlatButton( textColor: Theme.of(context).primaryColor, child: Text( 'Choose Date', style: TextStyle( fontWeight: FontWeight.bold, ), ), onPressed: _presentDatePicker, ) ], ), ), RaisedButton( onPressed: _submitData, child: Text( 'Add Transaction', ), textColor: Colors.white, color: Theme.of(context).primaryColor, ) ], ), ), ); } }
Do help me and the flutter community grow better. Thank you in advance.