mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This does a cleanup of the examples, removing all of the "section" markers and extra comments that we don't need anymore now that the samples are no longer in the source code. It also removes the --template arguments from the {@tool dartpad} and {@tool sample} directives, since those are no longer used. It converts two examples that I discovered were still embedded into linked examples in the examples folder. I didn't delete the templates from the snippets config folder yet, because there are still embedded samples in the dart:ui package from the engine that use them. Once dart:ui no longer uses the templates, they can be removed. I bumped the version of the snippets package to pick up a change that allows removal of the --template argument.
79 lines
2.1 KiB
Dart
79 lines
2.1 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// Flutter code sample for ErrorWidget
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() {
|
|
// Set the ErrorWidget's builder before the app is started.
|
|
ErrorWidget.builder = (FlutterErrorDetails details) {
|
|
// This is how to tell if you're in debug mode: Assertions are only executed in
|
|
// debug mode.
|
|
bool inDebug = false;
|
|
assert(() {
|
|
inDebug = true;
|
|
return true;
|
|
}());
|
|
// If we're in debug mode, use the normal error widget which shows the error
|
|
// message:
|
|
if (inDebug) {
|
|
return ErrorWidget(details.exception);
|
|
}
|
|
// In release builds, show a yellow-on-blue message instead:
|
|
return Container(
|
|
alignment: Alignment.center,
|
|
child: Text(
|
|
'Error!\n${details.exception}',
|
|
style: const TextStyle(color: Colors.yellow),
|
|
textAlign: TextAlign.center,
|
|
textDirection: TextDirection.ltr,
|
|
),
|
|
);
|
|
};
|
|
|
|
// Start the app.
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
static const String _title = 'ErrorWidget Sample';
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
bool throwError = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (throwError) {
|
|
// Since the error widget is only used during a build, in this contrived example,
|
|
// we purposely throw an exception in a build function.
|
|
return Builder(
|
|
builder: (BuildContext context) {
|
|
throw Exception('oh no, an error');
|
|
},
|
|
);
|
|
} else {
|
|
return MaterialApp(
|
|
title: MyApp._title,
|
|
home: Scaffold(
|
|
appBar: AppBar(title: const Text(MyApp._title)),
|
|
body: Center(
|
|
child: TextButton(
|
|
onPressed: () {
|
|
setState(() { throwError = true; });
|
|
},
|
|
child: const Text('Error Prone')),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|