image.error_builder.0.dart 1.39 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'package:flutter/material.dart';

7
/// Flutter code sample for [Image.errorBuilder].
8

9
void main() => runApp(const ErrorBuilderExampleApp());
10

11 12
class ErrorBuilderExampleApp extends StatelessWidget {
  const ErrorBuilderExampleApp({super.key});
13 14 15 16

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
17 18
      home: Scaffold(
        body: Center(
19
          child: ErrorBuilderExample(),
20 21
        ),
      ),
22 23 24 25
    );
  }
}

26 27
class ErrorBuilderExample extends StatelessWidget {
  const ErrorBuilderExample({super.key});
28 29 30 31 32 33 34 35 36 37 38

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(),
        borderRadius: BorderRadius.circular(20),
      ),
      child: Image.network(
        'https://example.does.not.exist/image.jpg',
39
        errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
40 41 42 43 44 45 46 47 48 49 50 51
          // Appropriate logging or analytics, e.g.
          // myAnalytics.recordError(
          //   'An error occurred loading "https://example.does.not.exist/image.jpg"',
          //   exception,
          //   stackTrace,
          // );
          return const Text('😢');
        },
      ),
    );
  }
}