Blogs By Shankar
Dart
Dart
  • Dart Tutorial by Dès Vu Technologies
  • hello
  • Dart Collection
    • collectionInDart
  • File Handeling
    • fileHandelingInDart
  • Functions
    • functionParameters
    • Types of Functions in Dart
    • Functions in Dart
    • Annonymous Function
      • AnnonymousFunction
    • Arrow Function
      • arrowFunctions
    • BuiltinFunctions
      • importantBuiltinFunctions
      • mathFunction
  • OOP With Dart
    • encapsulation
    • oopWithDart
    • Generic In Dart
      • generic
    • constructor
      • constructor
      • factory constructor
        • factoryConstructor
      • initializer list constructor
        • Initializer List Constructor:
    • async dart
      • asyncAndAwait
        • asyncAndAwait
      • future
        • future
      • streams
        • streams
  • Sync and Async dart
    • syncAndAsyncDart
  • controlFlow
    • controlFlow
  • dataTypes
    • Dart Built-In Data Types
    • TypeConversion
    • String
      • stringOperations
      • string_jnterpolation
    • operators
      • Operators In Dart
  • operators
    • operators
  • user_input
    • userInput
  • variablesAndConstants
    • scopeInDart
    • variableTypesInDart
    • variables
Powered by GitBook
On this page
  1. variablesAndConstants

scopeInDart

Scope in Dart refers to the region or context within which a variable or identifier is accessible and can be used. Dart has two main types of scope.
  1. Global Scope: Variables or identifiers declared in the global scope are accessible from anywhere in the Dart program, including within functions, classes, and other scopes. Global scope variables are defined outside of any function or class.

    // Global scope variable
    int globalVar = 42;
    
    void main() {
      // Accessing globalVar from within the main function
      print(globalVar);
    }
  2. Local Scope: Local scope refers to the context within a block of code, such as a function or a loop. Variables or identifiers declared within a local scope are only accessible within that scope. They are not visible or accessible from outside that scope.

    void main() {
      // Local scope variable
      int localVar = 10;
    
      // Accessing localVar from within the main function
      print(localVar);
    }

    In this example, localVar is a local scope variable within the main function.

Block Scope:

Dart has block scope, which means that variables declared within a block (inside curly braces {}) are only accessible within that block. This applies to if statements, for loops, and other code blocks:

void main() {
  if (true) {
    // Block scope variable
    int blockVar = 5;

    // Accessing blockVar within the block
    print(blockVar);
  }

  // Attempting to access blockVar outside the block will result in an error
  // print(blockVar); // Uncommenting this line would result in an error
}

In this example, blockVar is accessible only within the if block and cannot be accessed outside of it.

Function Scope:

Variables declared within a function are said to have function scope. They are accessible only within that function:

void main() {
  // Function scope variable
  int functionVar = 20;

  // Accessing functionVar within the main function
  print(functionVar);

  anotherFunction();
}

void anotherFunction() {
  // Attempting to access functionVar here will result in an error
  // print(functionVar); // Uncommenting this line would result in an error
}

In this example, functionVar is a variable with function scope, and it is accessible only within the main function. Attempting to access it in anotherFunction would result in an error.

Understanding scope is essential in Dart programming, as it helps you manage variable visibility and avoid naming conflicts between different parts of your code. Variables declared in narrower scopes take precedence over variables in broader scopes when there are naming conflicts.

PreviousvariablesAndConstantsNextvariableTypesInDart

Last updated 1 year ago