Is it wrong to declare simple variables like int, double, bool, var, String, List inside callback function? (Which is generally nested within the build
method.)
For, example, I have a MyCustomInputWidget
with a callback function getInputs
. Now, it is convenient to simply declare some variables within this getInputs
callback function, e.g. bool isParsingInputSuccessful = false;
while I doubt if even this simple declaration is not a recommended practice?
Sometimes, I use var myOutputWidget;
to assign widget based on certain conditions.
Can using such simple declarations in callback methods or build method still cause problem with garbage collection
, orphaned instances, etc.? Or is it okay if the declarations are simple int, dobule, bool, etc.?
Please attach some relevant sources, documentation with the answer.
Is it wrong to declare simple variables like int, double, bool, var, String, List inside callback function? (Which is generally nested within the build
method.)
For, example, I have a MyCustomInputWidget
with a callback function getInputs
. Now, it is convenient to simply declare some variables within this getInputs
callback function, e.g. bool isParsingInputSuccessful = false;
while I doubt if even this simple declaration is not a recommended practice?
Sometimes, I use var myOutputWidget;
to assign widget based on certain conditions.
Can using such simple declarations in callback methods or build method still cause problem with garbage collection
, orphaned instances, etc.? Or is it okay if the declarations are simple int, dobule, bool, etc.?
Please attach some relevant sources, documentation with the answer.
Share Improve this question edited Mar 18 at 11:48 rusty asked Mar 18 at 8:44 rustyrusty 4921 gold badge3 silver badges10 bronze badges3 Answers
Reset to default 1Simple declarations will cause no issue. It's good practice as long as those declarations are not stateful object that need proper disposal.
Also because of a function like the build method might be called frequently, you should avoid expensive computations there.
These are local variables, and they are automatically managed by Dart’s memory management system. Note that the garbage collector will end up deleting it once its no longer referenced anywhere. On the upside, no memory leaks. On the downside, each time the function runs (like a callback execution), the variables are re-created and do not retain any previous state.
Excerpt from Basic memory concepts: The Dart VM allocates memory for the object at the moment of the object creation, and releases (or deallocates) the memory when the object is no longer used (see Dart garbage collection).
The local variables with known size (e.g. bool, int) within a function are allocated to the stack memory. Those objects whose size and lifespan is not known at compile time are allocated to the heap memory. Objects allocated on the heap have a longer lifespan and are managed by Dart’s garbage collector. (source)
Once a function finishes executing, all its local variables go out of scope, and the function’s stack frame is removed, and the memory occupied becomes available for reuse.
A code sample, stated “good practice”, in official Flutter documentation using build variables
declaration. This code sample is exemplified under title - “How to fix leak prone code”.
// GOOD
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final handler = () => apply(theme);
useHandler(handler);
…
So, for simple types and objects, it is okay to declare them within build
method or callback functions’ block. We should avoid declaration in build when the objects use streams, timers, controllers, etc.