I am trying to implement the feature to add a list of emails input but the user. This is what i have so far
List<String> _notificationEmails =[];
var _notificationEmailsController = TextEditingController();
Widget _buildNotificationEmailsInput() {
return TextFormField(
controller: _notificationEmailsController,
style: inputTextStyle,
maxLines: null,
validator: (String value) {
print(value);
if (value.isEmpty) {
return 'Emails Required';
}
return null;
},
onChanged: (String value){
if(value.substring(value.length-1)==','){
print('here');
setState(() {
_notificationEmails.add(value.substring(0,value.length-1));
});
_notificationEmailsController.clear();
}
print(_notificationEmails);
},
);
}
My intended outcome is that when the user enters an email then adds a comma after it, the email is appended o the list and the input field cleared but i get a loop of the action resulting to this _notificationEmails.add(value.substring(0,value.length-1));
running to infinity.
Here are the logs
[ +3 ms] flutter: [[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], je@gm<…>
This goes on and on. What have I done wrong?
Update
Incase it helps, i have realised the issue is brought about because every time the _notificationEmailsController.clear();
is called, it triggers onChange()
thus the loop.
question from:
https://stackoverflow.com/questions/66060771/adding-list-of-items-using-textformfield-in-flutter