of<T> method Null safety

T? of<T>(
  1. BuildContext context,
  2. Type type
)

Returns the localized resources object of the given type for the widget tree that corresponds to the given context.

Returns null if no resources object of the given type exists within the given context.

This method is typically used by a static factory method on the type class. For example Flutter's MaterialLocalizations class looks up Material resources with a method defined like this:

static MaterialLocalizations of(BuildContext context) {
   return Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);
}

Implementation

static T? of<T>(BuildContext context, Type type) {
  assert(context != null);
  assert(type != null);
  final _LocalizationsScope? scope = context.dependOnInheritedWidgetOfExactType<_LocalizationsScope>();
  return scope?.localizationsState.resourcesFor<T?>(type);
}