capture static method

CapturedData capture({
  1. required BuildContext from,
  2. required BuildContext? to,
})

Implementation

static CapturedData capture(
    {required BuildContext from, required BuildContext? to}) {
  if (from == to) {
    return CapturedData._([]);
  }
  final data = <_InheritedData>[];
  final Set<Type> dataTypes = <Type>{};
  late bool debugDidFindAncestor;
  assert(() {
    debugDidFindAncestor = to == null;
    return true;
  }());

  from.visitAncestorElements(
    (ancestor) {
      if (ancestor == to) {
        assert(() {
          debugDidFindAncestor = true;
          return true;
        }());
        return false;
      }
      if (ancestor is InheritedElement && ancestor.widget is _InheritedData) {
        final _InheritedData dataWidget = ancestor.widget as _InheritedData;
        final Type dataType = dataWidget.dataType;
        if (!dataTypes.contains(dataType)) {
          dataTypes.add(dataType);
          data.add(dataWidget);
        }
      }
      return true;
    },
  );

  assert(debugDidFindAncestor,
      'The provided `to` context must be an ancestor of the `from` context.');

  return CapturedData._(data);
}