Lexical scope

Dart is a lexically scoped language, which means that the scope ofvariables is determined statically, simply by the layout of the code.You can “follow the curly braces outwards” to see if a variable is inscope.

Here is an example of nested functions with variables at each scopelevel:

  1. bool topLevel = true;
  2. void main() {
  3. var insideMain = true;
  4. void myFunction() {
  5. var insideFunction = true;
  6. void nestedFunction() {
  7. var insideNestedFunction = true;
  8. assert(topLevel);
  9. assert(insideMain);
  10. assert(insideFunction);
  11. assert(insideNestedFunction);
  12. }
  13. }
  14. }

Notice how nestedFunction() can use variables from every level, allthe way up to the top level.