Ad
What Is The Lexical Scope In Dart?
Basically I am going through the definition of closure functions which says -
A function that can be referenced with access to the variables in its lexical scope is called a closure
So I want to know this term lexical scope.
Ad
Answer
Dart is a lexically scoped language, which means that the scope of variables is determined statically, simply by the layout of the code. You can “follow the curly braces outwards” to see if a variable is in scope.
Here is an example of nested functions with variables at each scope level:
String topLevel = 'Hello';
void firstFunction() {
String secondLevel = 'Hi';
print(topLevel);
nestedFunction() {
String thirdLevel = 'Howdy';
print(topLevel);
print(secondLevel);
innerNestedFunction() {
print(topLevel);
print(secondLevel);
print(thirdLevel);
}
}
print(thirdLeve);
}
void main() => firstFunction();
This is a valid function, until the last print statement. The third-level variable is defined outside the scope of the nested function, because scope is limited to its own block or the blocks above it.
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad