Skip to content
pubscout

dart_scope_functions

2.0.0, released 2 months ago by bizjak.dev

Kotlin-inspired scope functions like also, let, takeIf, and run for executing code blocks on objects.

WASM-ready
$ flutter pub add dart_scope_functions
Source repository

What it does

Implements Kotlin-inspired scope functions in Dart to execute blocks of code within the context of an object. It provides extension methods and global functions to chain operations, evaluate conditions, and handle nullable values more expressively.

AI-written summary of version 2.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You want to chain multiple operations or transformations on an object fluently.
  • You prefer Kotlin-style utility functions like also, let, takeIf, and run in Dart code.

Skip it when

  • You prefer standard Dart conditional expressions and cascade notations over functional scope wrappers.
  • You are working with asynchronous blocks inside also where you expect the method to await the Future.

Strengths

  • Brings expressive Kotlin-style scope functions to Dart.
  • Supports method chaining and conditional object execution.
  • Includes inline pragmas for performance optimization across dart2wasm, dart2js, and VM.

Weaknesses

  • Let does not short-circuit automatically on null like Kotlin's safe call operator combined with let.
  • Synchronous limitations with async closures inside functions like also.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
0

Just add the dependency to pubspec.yaml and import the library.

import 'package:dart_scope_functions/dart_scope_functions.dart';

void main() {
  var result = 'Hello'.also((it) {
    print(it);
  }).let((it) {
    return it.length;
  });
  print(result); // Prints 5
}

Alternatives

Packages that do the same job and could replace dart_scope_functions.

  • gtk

    GTK utilities for Flutter Linux applications, providing settings and application lifecycle helpers.

  • async

    Utility classes and helpers that extend Dart's async library.

  • flutter_flavorizr

    A utility to easily create and configure application flavors in Flutter.

  • ffi

    Utilities for working with Foreign Function Interface (FFI) code in Dart.