I have converted my project to null-safety Dart 2. I have been working through issues since the conversion. Right now, I need to change this line
clientFNameController.text = widget.trxns!['clientFName'];
so that
clientFNameController.text = null;
This is the line where the controller is declared:
final clientNameController = TextEditingController();
I have tried this
clientFNameController?.text = widget.trxns!['clientFName'];
and this
clientFNameController.text = widget.trxns?['clientFName'];
How do I write the this line
clientFNameController.text = widget.trxns['clientFName'];
so this can be true
clientFNameController.text = null;
You cannot set the text
property of a TextEditingController
to null
, since the type of the setter is String
, not String?
.
You can use the empty string ""
however. For example, if you wanted your text field to have the text of widget.trxnw['clientFName']
if it was non-null, and be empty otherwise, you could have something like this:
String? clientFName = widget.trxnw['clientFName'];
if (clientFName == null) {
clientFNameController.text = "";
} else {
clientFNameController.text = clientFName;
}
This can be shortened to:
String clientFName = widget.trxns['clientFName'] ?? ""; // "if null" operator
clientFNameController.text = clientFName;
Or shortened even further to:
clientFNameController.text = widget.trxns['clientFName'] ?? "";