Dart Function Anonymous Callback Function

Dart Function Anonymous Callback Function

·

1 min read

Anonymous Callback Function

  • Callback is basically a function or a method that we pass as an argument into another function or a method to perform an action.
  • Example

  •   void main() {
        // Function that takes a callback function as an argument
        void performOperation(int a, int b, Function(int, int) callback) {
          int result = callback(a, b);
          print('Result of operation: $result');
        }
    
        // Define an anonymous function as the callback
        performOperation(5, 3, (x, y) {
          return x + y; // This is the anonymous function performing addition
        });
    
        performOperation(10, 4, (x, y) {
          return x * y; // This is another anonymous function performing multiplication
        });
      }
    
  • Output

  •   Result of operation: 8
      Result of operation: 40
    
      Exited.