扑打后卫资产图像

发布于 2025-02-13 03:27:08 字数 407 浏览 3 评论 0 原文

如果我要访问的资产映像无法加载,我想要后备资产图像。

如果此代码: image(image:AssetImage('images/myimage.png'))引发错误,我想显示一个后备,例如 image image(image:aSsetImage:'' images/halldackimage.png'))'

我阅读 /a>,但这对我不起作用。使用自定义资产捆绑包,即使现有图像也没有加载。

I want a fallback asset image if the asset image I'm trying to access can't be loaded.

If this code: Image(image: AssetImage('images/myImage.png')) throws an error, I would like to display a fallback, for example Image(image: AssetImage('images/fallbackImage.png'))'.

I read through this article on medium but that didn't work for me. With the custom Asset Bundle, not even the existing images got loaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2025-02-20 03:27:08

您可以使用 errorbuilder 属性:

如果在图像加载期间发生错误,则调用一个构建器函数。

在您的情况下(null Safe):

Image(
      image: AssetImage('path/to/image.jpg'),
      errorBuilder:
          (BuildContext context, Object exception, StackTrace? stackTrace) {
        return Image(
            image: AssetImage('path/to/fallback.jpg'));
      },
    )

这是一个完整的示例,直接从文档


import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: Scaffold(
        body: Center(
          child: MyStatelessWidget(),
        ),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @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',
        errorBuilder:
            (BuildContext context, Object exception, StackTrace? stackTrace) {
          // Appropriate logging or analytics, e.g.
          // myAnalytics.recordError(
          //   'An error occurred loading "https://example.does.not.exist/image.jpg"',
          //   exception,
          //   stackTrace,
          // );
          return const Text('ð¢');
        },
      ),
    );
  }
}

You can use the errorBuilder property:

A builder function that is called if an error occurs during image loading.

In your case (null safe):

Image(
      image: AssetImage('path/to/image.jpg'),
      errorBuilder:
          (BuildContext context, Object exception, StackTrace? stackTrace) {
        return Image(
            image: AssetImage('path/to/fallback.jpg'));
      },
    )

Here is a complete example taken straight from the documentation:


import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: Scaffold(
        body: Center(
          child: MyStatelessWidget(),
        ),
      ),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @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',
        errorBuilder:
            (BuildContext context, Object exception, StackTrace? stackTrace) {
          // Appropriate logging or analytics, e.g.
          // myAnalytics.recordError(
          //   'An error occurred loading "https://example.does.not.exist/image.jpg"',
          //   exception,
          //   stackTrace,
          // );
          return const Text('ð¢');
        },
      ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文