synchronized
3.6.1+1, released 11 days ago by tekartik.com
Lock mechanism to prevent concurrent access to asynchronous code.
What it does
Provides a basic Lock, ReentrantLock, MultiLock, and GlobalLock to ensure that critical sections of asynchronous operations run sequentially within a single isolate or globally across processes. Preserves return values and handles timeouts when acquiring locks.
AI-written summary of version 3.6.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need to provide transactions on database systems lacking built-in transaction mechanisms like MongoDB or file systems
- You want to ensure asynchronous UI operations in a web app do not conflict
- You need cross-isolate, cross-process, or cross-browser tab synchronization using GlobalLock
Skip it when
- You need cross-process synchronization and are running on node (where GlobalLock is limited to the current process)
Strengths
- No dependencies other than the Dart SDK
- Supports browser, DartVM, and Flutter across multiple platforms
- Offers reentrant lock options via Zones and multi-lock capabilities
Weaknesses
- The name is biased since Dart is single-threaded; it only helps handle asynchronous await interleaving
- Basic locks are not reentrant by default
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 0
import 'package:synchronized/synchronized.dart';
Future<void> main() async {
var lock = Lock();
await lock.synchronized(() async {
// Only this block can run (once) until done
});
}Alternatives
Packages that do the same job and could replace synchronized.
- async_locks
Synchronization primitives for asynchronous Dart code like Lock, Event, and Semaphore
53Compare - rx
Reactive Extensions library for Dart offering observable streams and async operators.
72Compare - queue_it
A Dart queue with concurrency, retries and event listeners
71Compare - http_status_code
A Dart library providing constants and helpers for HTTP status codes.
39Compare
Works with
Packages commonly used together with synchronized: ones it builds on, ones built on it, and its own family.
- sqflite_commonBuilt on synchronizedDart wrapper on SQLite providing the common API for sqflite and desktop FFI implementations.
- audioplayersBuilt on synchronizedA Flutter plugin to play multiple audio files simultaneously
- just_audioBuilt on synchronizedFeature‑rich audio player for Flutter supporting gapless playlists and multiple sources
- just_audio_webBuilt on synchronizedWeb platform implementation of just_audio for audio playback.
- dart_webrtcBuilt on synchronizedWebRTC API wrapper for Dart, enabling browser-based audio/video communication.
- pdfrxBuilt on synchronizedFast, cross‑platform PDF viewer and editor widget for Flutter apps.
- sqflite_common_ffiBuilt on synchronizedFFI-based implementation of sqflite for desktop platforms and unit tests.
- pdfrx_engineBuilt on synchronizedPure Dart PDF rendering and manipulation API built on top of PDFium.