ptr 0.0.0 copy "ptr: ^0.0.0" to clipboard
ptr: ^0.0.0 copied to clipboard

A lightweight Dart package for managing lazy, overridable object references.

example/ptr_example.dart

import 'package:ptr/ptr.dart';

// Create a getter to access the pointer reference.
Counter get counter => $(Counter.ref);

abstract interface class Counter {
  // Declare a pointer reference to the instance.
  static final ref = ptr<Counter>(RealCounter.new);

  int get value;

  void increment();
}

class RealCounter implements Counter {
  RealCounter() : _value = 0;

  int _value;

  int get value => _value;

  void increment() => _value + 1;
}

class FakeCounter implements Counter {
  @override
  int get value => 42;

  @override
  void increment() {}
}

void main() {
  // Interact with reference.
  print(counter.value); // 0
  counter.increment(); // increments the count.
  print(counter.value); // 1

  // Override the pointer reference.
  Counter.ref.override(FakeCounter.new);

  // Interact with the overridden reference.
  print(counter.value); // 42
  counter.increment(); // does nothing in the fake instance.
  print(counter.value); // 42
}
1
likes
150
points
8
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight Dart package for managing lazy, overridable object references.

Documentation

API reference

License

MIT (license)

Dependencies

meta

More

Packages that depend on ptr