Using controllers inside StatelessWidget

Viewed 294

Normally we dispose all controllers in StatefulWidget's dispose method.

But how if we are using StatelessWidget? is this going to automatically disposed?

Or we should never use controllers/streams inside StatelessWidget?

Thanks!

1 Answers

You do not have the initState and dispose methods in a stateless widget like the stateful widgets.

Yet, there are ways of using stateless widgets with resources that need to be allocated and disposed to avoid memory leaks. You can check out one of these packages:

There are examples provided on the page that will help you understand the packages better.

I would recommend learning Providers if you don't know already as it will open a lot of ways you can manage states in your application efficiently and in a clean way.

Flutter Hooks may be a bit confusing to understand at first but, if you are familiar with useHooks in React, it is similar.

Related