top<T extends TopModel> function

T top<T extends TopModel>()

Retrieve a TopModel from the app root.

This is a convenience function that works from any BuildContext-free context (e.g. inside Logic, Model methods, or utility functions).

final appModel = top<AppModel>();

To read within a widget, prefer MoReadContext.read() or MoWatchContext.watch() for type-safe context-based access.

Throws if no TopProvider is mounted. Check TopModel.isReady first.

Implementation

T top<T extends TopModel>() {
  final ctx = _TopProviderState._currentContext;
  if (ctx == null) {
    throw FlutterError.fromParts([
      ErrorSummary('top<$T>() called before a TopProvider was mounted.'),
      ErrorDescription(
        'Wrap your app or test root with TopProvider(...) before calling '
        'top<$T>().',
      ),
    ]);
  }
  return ctx.read<T>();
}