Search
 
SCRIPT & CODE EXAMPLE
 

DART

dart what is a closure

1
Dart Closures Tutorial with Examples
https://o7planning.org/14061/dart-closures

2
Closures

A function can be created in the global scope or within the scope of another 
function. A function that can be referenced with an access to the variables 
in its lexical scope is called a closure, as shown in the following code:

<--

library function_closure;

// Function returns closure function.
calculate(base) {
  // Counter store
  var count = 1;
  // Inner function - closure
  return () => print("Value is ${base + count++}");
}

void main() {
  // The outer function returns inner
  var f = calculate(2);
  // Now we call closure
  f();
  f();
}

-->

Here is the result of the preceding code:

Value is 3
Value is 4

We have the calculate function, which contains the count variable and returns
a an inner function. The inner function has an access to the count variable 
because both are defined in the same scope. The count variable exists only 
within the scope of calculate and would normally disappear when the function 
exits. This does not happen in this case because the inner function returned 
by calculate holds a reference to count. The variable has been closed covered,
meaning it's within a closure.
Comment

PREVIOUS NEXT
Code Example
Dart :: convert iso date string into date and time string flutter 
Dart :: dart setters 
Dart :: Bad state: Stream has already been listened to 
Dart :: dart string to hex 
Dart :: android studio avd blue screen 
Dart :: flutter run in background every second 
Dart :: cupertino icons flutter 
Dart :: alertdialog shape flutter 
Dart :: flutter clipoval 
Dart :: get initials from name flutter on text 
Dart :: best visual studio code extensions for flutter development 
Dart :: flutter list 
Dart :: flutter tooltip 
Dart :: perform async task when build is done flutter 
Dart :: srring reverse dart 
Dart :: flutter add icon 
Dart :: change name of flutter app 
Dart :: get the type of an object dart 
Dart :: dart list equality 
Dart :: flutter how to execute function after building screen 
Dart :: Array of colors in dart 
Dart :: increase widh of TableCell in flutter 
Dart :: flutter button playing sound 
Dart :: flutter get child widget size 
Dart :: search functionality dart 
Dart :: flutter outline button overlay 
Dart :: dart test matcher expecting a field value 
Dart :: flutter compare two list 
Dart :: how to show snackbar in initState() in flutter 
Swift :: random string swift 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =