FlutterError.fromParts constructor

FlutterError.fromParts(List<DiagnosticsNode> diagnostics)

Create an error message from a list of DiagnosticsNodes.

By convention, there should be exactly one FlutterSummary in the list, and it should be the first entry.

Other entries are typically ErrorDescriptions (for material that is always applicable for this error) and ErrorHints (for material that may be sometimes useful, but may not always apply). Other DiagnosticsNode subclasses, such as DiagnosticsStackTrace, may also be used.

Implementation

FlutterError.fromParts(this.diagnostics) : assert(diagnostics.isNotEmpty, FlutterError.fromParts(<DiagnosticsNode>[ErrorSummary('Empty FlutterError')])) {
  assert(
    diagnostics.first.level == DiagnosticLevel.summary,
    FlutterError.fromParts(<DiagnosticsNode>[
      ErrorSummary('FlutterError is missing a summary.'),
      ErrorDescription(
        'All FlutterError objects should start with a short (one line) '
        'summary description of the problem that was detected.'
      ),
      DiagnosticsProperty<FlutterError>('Malformed', this, expandableValue: true, showSeparator: false, style: DiagnosticsTreeStyle.whitespace),
      ErrorDescription(
        '\nThis error should still help you solve your problem, '
        'however please also report this malformed error in the '
        'framework by filing a bug on GitHub:\n'
        '  https://github.com/flutter/flutter/issues/new?template=BUG.md'
      ),
    ],
  ));
  assert(() {
    final Iterable<DiagnosticsNode> summaries = diagnostics.where((DiagnosticsNode node) => node.level == DiagnosticLevel.summary);
    if (summaries.length > 1) {
      final List<DiagnosticsNode> message = <DiagnosticsNode>[
        ErrorSummary('FlutterError contained multiple error summaries.'),
        ErrorDescription(
          'All FlutterError objects should have only a single short '
          '(one line) summary description of the problem that was '
          'detected.'
        ),
      ];
      message.add(DiagnosticsProperty<FlutterError>('Malformed', this, expandableValue: true, showSeparator: false, style: DiagnosticsTreeStyle.whitespace));
      message.add(ErrorDescription('\nThe malformed error has ${summaries.length} summaries.'));
      int i = 1;
      for (DiagnosticsNode summary in summaries) {
        message.add(DiagnosticsProperty<DiagnosticsNode>('Summary $i', summary, expandableValue : true));
        i += 1;
      }
      message.add(ErrorDescription(
        '\nThis error should still help you solve your problem, '
        'however please also report this malformed error in the '
        'framework by filing a bug on GitHub:\n'
        '  https://github.com/flutter/flutter/issues/new?template=BUG.md'
      ));
      throw FlutterError.fromParts(message);
    }
    return true;
  }());
}