I am trying to dynamically retrieve TextControllers text value. I created an object like so:
final _ingredientsDataMap = {
'IngredientName': {
'label': 'Ingredient Name',
'controller': new TextEditingController(),
},
'Amount': {
'label': 'Amount',
'controller': new TextEditingController(),
},
'Unit': {
'label': 'Unit',
'controller': new TextEditingController(),
},
};
and a function for testing purposes
_submitData() {
for (var k in _ingredientsDataMap.keys) {
var currentValue = _ingredientsDataMap[k];
var controller = currentValue!['controller'];
// Neither way works
print(controller!['text']);
print(controller!.text;
}
}
However, I get an error as soon as I add text to the end of the controller variable and of course the program will not compile: The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'
What am I doing wrong here? Is my initial map the culprit? I suspect there is something I am missing (I come from a javascript background).
Also including a DartPad implementation that works as long as you comment out the print line: https://dartpad.dev/?id=5969246a632f388bf32e8649620a2c34&null_safety=true