2

The question is simple & hard to understand at the same time. Why do I need to mark all my variables in the Statefull Widget as private?

Now when I declare a variable I declare it like private.

bool _isOpened = true;
String _currentUserUID;
...

But why do I need this? I do not access these variables from other widgets. I do not override. I know my app logic & totally insured in the safety of my code in all widgets.

So why do I need private variables? Is there any impact of using private variables & methods? Maybe performance benefits?

1 Answer 1

5

The prefix _ makes the variable private within the .dart code file you are in. The IDE (or at least VSCode, that I use) will then be able to tell you if you are using the variable or not. It also tells me, as the developer, that I need look no further than the code file that I am working in for changes to this variable. All important information as far as I am concerned.

The same is true when making methods private with the _ prefix. This is also highly recommended.

However, imho, this question relates to the the use of private variables in general so here is a link to a more general question and a much better set of answers than mine https://softwareengineering.stackexchange.com/questions/143736/why-do-we-need-private-variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.