Dart Flutter Tips

Closure with partial application

Sometimes it is convenient to use closure (method inside a method) with partial application to reuse certain functionality.
Plague Fox 1 min read
Closure with partial application
Photo by Artur Shamsutdinov / Unsplash

Sometimes it is convenient to use closure (method inside a method) with partial application to reuse certain functionality.

import 'dart:developer' as developer;

/// Tracing information
final void Function(Object? message) fine = _logAll('FINE', 500);

/// Static configuration messages
final void Function(Object? message) config = _logAll('CONF', 700);

/// Iformational messages
final void Function(Object? message) info = _logAll('INFO', 800);

/// Potential problems
final void Function(Object exception, [StackTrace? stackTrace, String? reason])
    warning = _logAll('WARN', 900);

/// Serious failures
final void Function(Object error, [StackTrace stackTrace, String? reason])
    severe = _logAll('ERR!', 1000);

void Function(
  Object? message, [
  StackTrace? stackTrace,
  String? reason,
]) _logAll(String prefix, int level) =>
    (Object? message, [StackTrace? stackTrace, String? reason]) {
      if (!kLogEnabled) return;
      developer.log(
        '[$prefix] ${reason ?? message}',
        level: level,
        name: 'isolation',
        error: message is Exception || message is Error ? message : null,
        stackTrace: stackTrace,
      );
    };
Share
Comments
More from Plague Fox

Plague Fox

Engineer by day, fox by night. Talks about Flutter & Dart.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Plague Fox.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.