debug.dart 6.83 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';

import 'material.dart';
8
import 'material_localizations.dart';
9
import 'scaffold.dart' show Scaffold, ScaffoldMessenger;
10

11 12 13
// Examples can assume:
// late BuildContext context;

14
/// Asserts that the given context has a [Material] ancestor.
15
///
16
/// Used by many Material Design widgets to make sure that they are
17 18
/// only used in contexts where they can print ink onto some material.
///
19
/// To call this function, use the following pattern, typically in the
20
/// relevant Widget's build method:
21 22 23 24 25
///
/// ```dart
/// assert(debugCheckHasMaterial(context));
/// ```
///
26 27 28 29
/// Always place this before any early returns, so that the invariant is checked
/// in all cases. This prevents bugs from hiding until a particular codepath is
/// hit.
///
30 31
/// This method can be expensive (it walks the element tree).
///
32
/// Does nothing if asserts are disabled. Always returns true.
33 34
bool debugCheckHasMaterial(BuildContext context) {
  assert(() {
35
    if (context.widget is! Material && context.findAncestorWidgetOfExactType<Material>() == null) {
36 37 38 39 40
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No Material widget found.'),
        ErrorDescription(
          '${context.widget.runtimeType} widgets require a Material '
          'widget ancestor.\n'
41
          'In Material Design, most widgets are conceptually "printed" on '
42
          "a sheet of material. In Flutter's material library, that "
43 44 45
          'material is represented by the Material widget. It is the '
          'Material widget that renders ink splashes, for instance. '
          'Because of this, many material library widgets require that '
46
          'there be a Material widget in the tree above them.',
47 48 49 50 51 52
        ),
        ErrorHint(
          'To introduce a Material widget, you can either directly '
          'include one, or use a widget that contains Material itself, '
          'such as a Card, Dialog, Drawer, or Scaffold.',
        ),
53 54
        ...context.describeMissingAncestor(expectedAncestorType: Material),
      ]);
Hixie's avatar
Hixie committed
55 56
    }
    return true;
57
  }());
58 59
  return true;
}
60 61 62 63

/// Asserts that the given context has a [Localizations] ancestor that contains
/// a [MaterialLocalizations] delegate.
///
64
/// Used by many Material Design widgets to make sure that they are
65 66 67 68 69 70 71 72 73
/// only used in contexts where they have access to localizations.
///
/// To call this function, use the following pattern, typically in the
/// relevant Widget's build method:
///
/// ```dart
/// assert(debugCheckHasMaterialLocalizations(context));
/// ```
///
74 75 76 77
/// Always place this before any early returns, so that the invariant is checked
/// in all cases. This prevents bugs from hiding until a particular codepath is
/// hit.
///
78 79 80 81 82
/// This function has the side-effect of establishing an inheritance
/// relationship with the nearest [Localizations] widget (see
/// [BuildContext.dependOnInheritedWidgetOfExactType]). This is ok if the caller
/// always also calls [Localizations.of] or [Localizations.localeOf].
///
83 84 85 86
/// Does nothing if asserts are disabled. Always returns true.
bool debugCheckHasMaterialLocalizations(BuildContext context) {
  assert(() {
    if (Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) == null) {
87 88 89 90
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No MaterialLocalizations found.'),
        ErrorDescription(
          '${context.widget.runtimeType} widgets require MaterialLocalizations '
91
          'to be provided by a Localizations widget ancestor.',
92 93
        ),
        ErrorDescription(
94
          'The material library uses Localizations to generate messages, '
95
          'labels, and abbreviations.',
96 97 98 99 100
        ),
        ErrorHint(
          'To introduce a MaterialLocalizations, either use a '
          'MaterialApp at the root of your application to include them '
          'automatically, or add a Localization widget with a '
101
          'MaterialLocalizations delegate.',
102
        ),
103
        ...context.describeMissingAncestor(expectedAncestorType: MaterialLocalizations),
104
      ]);
105 106 107 108 109
    }
    return true;
  }());
  return true;
}
110 111 112 113 114 115 116 117 118 119 120 121 122

/// Asserts that the given context has a [Scaffold] ancestor.
///
/// Used by various widgets to make sure that they are only used in an
/// appropriate context.
///
/// To invoke this function, use the following pattern, typically in the
/// relevant Widget's build method:
///
/// ```dart
/// assert(debugCheckHasScaffold(context));
/// ```
///
123 124 125 126
/// Always place this before any early returns, so that the invariant is checked
/// in all cases. This prevents bugs from hiding until a particular codepath is
/// hit.
///
127 128
/// This method can be expensive (it walks the element tree).
///
129 130 131
/// Does nothing if asserts are disabled. Always returns true.
bool debugCheckHasScaffold(BuildContext context) {
  assert(() {
132
    if (context.widget is! Scaffold && context.findAncestorWidgetOfExactType<Scaffold>() == null) {
133 134 135 136 137
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No Scaffold widget found.'),
        ErrorDescription('${context.widget.runtimeType} widgets require a Scaffold widget ancestor.'),
        ...context.describeMissingAncestor(expectedAncestorType: Scaffold),
        ErrorHint(
138
          'Typically, the Scaffold widget is introduced by the MaterialApp or '
139 140
          'WidgetsApp widget at the top of your application widget tree.',
        ),
141
      ]);
142 143 144 145 146
    }
    return true;
  }());
  return true;
}
147 148 149 150 151 152 153 154 155 156 157 158 159

/// Asserts that the given context has a [ScaffoldMessenger] ancestor.
///
/// Used by various widgets to make sure that they are only used in an
/// appropriate context.
///
/// To invoke this function, use the following pattern, typically in the
/// relevant Widget's build method:
///
/// ```dart
/// assert(debugCheckHasScaffoldMessenger(context));
/// ```
///
160 161 162 163
/// Always place this before any early returns, so that the invariant is checked
/// in all cases. This prevents bugs from hiding until a particular codepath is
/// hit.
///
164 165
/// This method can be expensive (it walks the element tree).
///
166 167 168 169 170 171 172 173 174 175
/// Does nothing if asserts are disabled. Always returns true.
bool debugCheckHasScaffoldMessenger(BuildContext context) {
  assert(() {
    if (context.findAncestorWidgetOfExactType<ScaffoldMessenger>() == null) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No ScaffoldMessenger widget found.'),
        ErrorDescription('${context.widget.runtimeType} widgets require a ScaffoldMessenger widget ancestor.'),
        ...context.describeMissingAncestor(expectedAncestorType: ScaffoldMessenger),
        ErrorHint(
          'Typically, the ScaffoldMessenger widget is introduced by the MaterialApp '
176 177
          'at the top of your application widget tree.',
        ),
178 179 180 181 182 183
      ]);
    }
    return true;
  }());
  return true;
}