ReactiveNotifier<T> constructor

ReactiveNotifier<T>(
  1. T create(), {
  2. List<ReactiveNotifier>? related,
  3. Key? key,
  4. bool autoDispose = false,
})

Creates or returns existing instance of ReactiveNotifier

Parameters:

  • create: Function that creates the initial state
  • related: Optional list of related states
  • key: Optional key for instance identity

Implementation

factory ReactiveNotifier(T Function() create,
    {List<ReactiveNotifier>? related, Key? key, bool autoDispose = false}) {
  key ??= UniqueKey();

  assert(() {
    log('''
📦 Creating ReactiveNotifier<$T>
${related != null ? '🔗 With related types: ${related.map((r) => r.notifier.runtimeType).join(', ')}' : ''}
''', level: 5);
    return true;
  }());

  if (_instances.containsKey(key)) {
    final trace = StackTrace.current.toString().split('\n')[1];
    throw StateError('''
⚠️ Invalid Reference Structure Detected!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Current Notifier: $T
Key: $key
Problem: Attempting to create a notifier with an existing key, which could lead to circular dependencies or duplicate instances.
Solution: Ensure that each notifier has a unique key or does not reference itself directly.
Location: $trace
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
''');
  }

  try {
    _instances[key] = ReactiveNotifier._(create, related, key, autoDispose);
  } catch (e) {
    if (e is StateError) {
      rethrow;
    }
    final trace = StackTrace.current.toString().split('\n')[1];
    throw StateError('''
⚠️ ReactiveNotifier Creation Failed!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Type: $T
Error: $e

🔍 Check:
 - Related states configuration
 - Initial value creation
 - Type consistency
Location: $trace
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
''');
  }

  return _instances[key] as ReactiveNotifier<T>;
}