flutter 图像资产在Web上加载良好,但在移动设备上加载不佳

fgw7neuy  于 2023-02-05  发布在  Flutter
关注(0)|答案(2)|浏览(138)

我的Flutter应用程序有一个问题。我正在尝试在我的SplashWidget上加载一个简单的图像,如下所示。
当我做flutter run -d chrome的图像加载完全罚款在网络上(谷歌浏览器)。
现在,当我测试我的真正的Android设备(华为EVA LO9)运行在棉花糖(Android 6.0)我得到下面的错误:

══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE
╞════════════════════════════════════════════════════
                    The following assertion was thrown resolving an image codec:
                    Unable to load asset: images/splash_icon.png
                    
                    When the exception was thrown, this was the stack:
                    #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:227:7)
                    <asynchronous suspension>
                    #1      AssetBundleImageProvider._loadAsync
(package:flutter/src/painting/image_provider.dart:667:14)
                    <asynchronous suspension>
                    ...
                    
                    Image provider: AssetImage(bundle: null, name: "images/splash_icon.png")
                    Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#d1d58(), name:
                      "images/splash_icon.png", scale: 1.0)

这是我的SplashWiget

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class SplashWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      decoration: BoxDecoration(color: BrandColors.PRIMARY_COLOR),
      child: Center(
        child: Image(
          image: AssetImage("images/splash_icon.png"),
        ),
      ),
    );
  }
}

这是我的pubspec.yaml

flutter:
  uses-material-design: true
  assets:
    - assets/

下面是我的目录结构的屏幕截图

我需要做任何android的特定配置来让这个映像加载到一个真正的android设备上吗?我不敢相信我在这样一个简单的事情上浪费了几个小时。这太累人了。
谁来帮帮我。

9jyewag0

9jyewag01#

试着这样做:

Container(
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage("images/splash_icon.png"),
        fit: BoxFit.cover,
      ),
    ),
  )

首先,您要查看它是否运行良好,然后应用您的逻辑/风格。
如果不行就告诉我。
也试着做一个干净的扑动。然后扑动运行;)

e1xvtsh3

e1xvtsh32#

对我来说,我发现路径需要在代码中与pubspec.yaml完全相同
所以在你pubspec.yaml里是这样的:

assets:
- assets/images/

在你的代码中你想要加载图像的地方如下:

image: AssetImage('assets/images/image1.jpg')

相关问题