I created a PostUpdaterWidget
extending StatelessWidget
which makes use of TextEditingControllers
for testing out implementation of Bloc Pattern.
final _usernameController = TextEditingController();
final _contentController = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextField(
controller: _usernameController,
decoration: InputDecoration(hintText: "Post Username"),
),
TextField(
controller: _contentController,
decoration: InputDecoration(hintText: "Post Content"),
),
Container(
height: 16,
),
RaisedButton(
child: Text("Update Post"),
onPressed: () => _updatePost(context),
)
],
);
}
_updatePost(BuildContext context) {
print("Processing Post Update");
String username = _usernameController.text.trim();
String content = _contentController.text.trim();
Post post = new Post();
post.id = id;
post.username = username;
post.content = content;
id += 1;
print("Dispatching Post Update");
BlocProvider.of<PostBloc>(context).updatePost(post);
}
I have seen in a lot of examples that controllers should be disposed. However there is no method to override
a dispose
function in a StatelessWidget
.
I have thought of creating its own dispose function to dispose the controllers used, and just create a variable of this widget for those that will use this widget so that I can call the dispose function.
But I want to know first whether I really need to do that, or this StatelessWidget actually disposes on its own.
Should I proceed with my idea? Or just leave it be, since it might be disposing these controllers on its own, so that I should not be concerned of memory leaks.
StatelessWidget
do it by itself when garbage collected? – Dugas