Proxy Pattern in Dart: Lazy Loading & Access Control

The proxy pattern in Dart puts a stand-in object in front of the real one to add lazy loading, access control, caching, or logging — without changing the original class.

A proxy is a stand-in object. It implements the same interface as the real thing, the client can't tell them apart, and behind the scenes it adds what the original class never knew about: deferring expensive work until needed, checking permissions, caching repeated results, logging requests, or hiding that the "object" is a server across the network. That's the proxy pattern in Dart in one paragraph — same interface, extra behavior, zero changes to the original class. This proxy pattern Dart guide builds all five flavors in runnable Dart 3.13: virtual, protection, caching, logging, and remote proxies — centered on a caching proxy around a Supabase-style service that saves real API calls. What you'll build: five proxy flavors in Dart A mental model: client → proxy → real subject, and what each flavor intercepts A virtual proxy that lazily loads an expensive image with Dart's ??= A protection proxy enforcing role-based access on a document service A caching proxy over a Supabase-shaped backend that eliminates duplicate network calls A logging proxy measuring request timing without touching the real class A remote-proxy sketch for platform-channel-style boundaries A decision table separating proxy from decorator and adapter All code targets Dart 3.13 (ships with Flutter 3.47 ) and runs with plain dart run . The Supabase client is faked with identical method shapes so the example runs without credentials. Virtual proxy: lazy loading that actually defers work The virtual proxy is the most famous flavor: a lightweight stand-in for an expensive object, created eagerly but loading the real thing only on first use. Dart's ??= operator makes this almost embarrassingly concise: // Dart 3.13 — paste into main.dart and run with `dart run`. // No external…

Explore