admin管理员组文章数量:1395730
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.
本文标签:
版权声明:本文标题:flutter - Is it wrong to declare simple variables like int, double, bool, var, String, List inside callback function? - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744515370a2610130.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论