A Dart visibilty primer for Java devs

A couple of sentences about visibility and scope

Thomas Künneth
Dec 18, 2019 · 2 min read
Two pairs of glasses
Some sentences about visibility

If you are new to Dart and Flutter, you may have wondered why some identifiers start with an underscore. As you will see shortly, this is related to visibility and scope. The underlying questions are

  • Where can I see a variable, function or method?
  • Where and when can I access them?

To start with, consider the following source file named a.dart. Yes, no underscore… :-)

var answer = 42;
main() {
var a = answer;
{
var b = a;
print(b);
}
print(b);
}

It will not work. If you try to run it, you get Error: Getter not found: ‘b’. To a Java developer it will not be surprising that print(b); outside the nested block fails, as b is not visible here. But why do we see Getter not found? Didn’t I just access an identifier? In Dart access to variables occurs via getters and setters, and if these have not been defined by the developer, they will be there automatically. If we remove the second print(), the program will run just fine (assume a version named a_correct.dart).

Now take a look at the following lines of code:

import “a_correct.dart”;
main() {
print(answer);
}

If we run it, 42 is printed once. In a_correct.dart answer has been declared at the top-level, so the identifier can be seen by the dart file (to be more precisely: library) that imports a_correct.dart. But what if we don’t want a variable or function to be visible?

Just prefix the identifier with an underscore.

Imagine a file b.dart:

var _answer = 42;
_hello() {
print(_answer);
}

And c.dart:

import “b.dart”;
main() {
print(_answer);
_hello();
}

Not to much surprise, it will fail. Neither _hello() nor _answer are visible. There is more control about what packages export to the outside world, but this is an advanced topic I might cover later. So, for now dear Java devs, just forget about public, private and protected. And stick to this simple rule:

If it’s local, prefix the identifier with an underscore.

Clear. Precise.

A previous version of this article was published 2017/02/06 on my Blogger.com blog Tommis Blog. I deleted the weblog in the wake of the GDPR (General Data Protection Regulation), so the original post is no longer available (or only through the Wayback machine of the Internet Archive).

If not stated otherwise images are © Thomas Künneth

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store