Try changing the type of the variable, or casting the right-hand type to 'String'.
TextEditingController dateInput = TextEditingController();
@override
void initState() {
super.initState();
if (widget.initialValue != null) {
dateInput.text = widget.initialValue;
}
}
error A value of type 'DateTime?' can't be assigned to a variable of type 'String'. Try changing the type of the variable, or casting the right-hand type to 'String'.
CodePudding user response:
You can use DateFormat
from intl package to format your DateTime
Object to whatever format you want to display.
import 'package:intl/intl.dart';
TextEditingController dateInput = TextEditingController();
@override
void initState() {
super.initState();
if (widget.initialValue != null) {
// Format date to whichever format you want to display
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(widget.initialValue);
dateInput.text = formattedDate;
}
}
CodePudding user response:
You can change the initial value to string by using the toString() function like this:
dateInput.text = widget.initialValue.toString();