パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.0.5

using

自動クリーンアップと最終化により、クラスを強化します。C# の "using + IDisposable" や Java の "try-with-resource + AutoCloseable" に似ています。

AndroidiOSLinuxmacOSWebWindows
30日間ダウンロード
3.2万
いいね数
4
Pub ポイント
160

パッケージをインストール

flutter pub add using

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

The 'using' package enhances Dart classes with automatic resource cleanup and finalization, inspired by C#'s 'using' statement and Java's 'try-with-resources'. It enables developers to safely manage resources like files, streams, or connections by automatically calling a close method when the object goes out of scope. This reduces boilerplate code and prevents resource leaks. The package integrates seamlessly with Flutter and supports all major platforms, making it ideal for writing clean, safe, and maintainable code.

用途

  • Managing file and stream resources
  • Automating disposal of network connections
  • Simplifying lifecycle management in widgets
  • Ensuring cleanup after async operations
  • Preventing memory leaks in long-running apps

主な機能

  • Automatic resource cleanup
  • Supports async and sync disposal
  • Works across all Flutter platforms
  • Minimal boilerplate
  • Intuitive syntax similar to C# and Java

適している対象

  • Developers seeking C#-style using statements
  • Teams focused on resource safety
  • Flutter apps requiring reliable cleanup
  • Complex widget hierarchies with disposable dependencies
  • Codebases aiming to reduce manual dispose calls

注意事項

  • Requires understanding of dispose patterns
  • May add slight overhead for simple cases
  • Best used with classes implementing IDisposable
  • Not needed for trivial or short-lived resources
  • Limited to objects that support auto-cleanup

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Using

This package provides a Releasable mixin to upgrade classes with automatic resource cleanup and finalization features.

Examples

class Resource with Releasable {
  // manage resource

  @override
  void release() {
    // clean-up resource in this method
    // and make sure super.release() is called!
    super.release();
  }
}

void main() {
  Resource? res;
  try {
    res = Resource(); // allocate
    // use resource
  } finally {
    // release in a finally block to ensure resources are released
    // even when an error occurs
    res?.release();
  }
}

Extension methods are provided to support syntaxes similar to C#'s using and Java's try-with-resource. They also help make sure resources are properly released!

void main() {
  final data = Resource().use((res) {
    // use `res` to load and return some data
  });
  // the instance created by `Resource()` is guaranteed to be released

  final asyncData = await Resource().useAsync((res) async {
    // use `res` to load and return some data
  });
  // the instance created by `Resource()` is guaranteed to be released
}

Streams are also supported via the consume() extension method:

void main() {
  final outputs = await Resource().consume(inputStream, (res, input) async {
    // use `res` to process inputs and return outputs
  }).toList();
  // the instance created by `Resource()` is guaranteed to be released
}

Automatic finalization is also available to try ensure resources will be released even in situations where release() has not been called. However, no promises are made that this will really happen. For more information, see Dart's documentation for Finalizer.

class ResourceOwner with Releasable {
  final _res1 = Resource();
  final _res2 = Resource();

  ResourceOwner() {
    // non-deterministic clean-up relying on Dart's `Finalizer` class. If
    // the resource owner goes out of scope and `release()` was not called,
    // the two resources should eventually be released and reclaimed by a
    // `Finalizer`.
    autoRelease([_res1, _res2]);
  }

  // deterministic clean-up by explicitly calling `release()` on this
  // instance, or when the instance is used via extension methods (`use()`,
  // `consume()`...).
  @override
  void release() {
    // clean-up resources
    _res1.release();
    _res2.release();
    // call super method
    super.release();
  }
}

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。

ランキング