【问题标题】:Flutter: Images missing when buildFlutter:构建时缺少图像
【发布时间】:2020-04-01 22:05:28
【问题描述】:

我的颤振项目有问题。我在 pubspec.yaml 中添加了一些图像、声音和 json 文件,如下所示:

 flutter:

  uses-material-design: true

  assets:
    - assets/
    - assets/images/
    - assets/audio/

json 在 assets/ 中,背景和一些图标在 assets/images/ 中。 运行 flutter run 时一切正常。运行flutter build并在设备上安装apk时,背景可用,图标不可用。

示例输出:

I/flutter (20572): Image provider: AssetImage(bundle: null, name: "assets/images/background.png")                  
I/flutter (20572): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#16f51(), name:                       
I/flutter (20572):   "assets/images/background.png", scale: 1.0)                                                   
I/flutter (20572): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (20572): Another exception was thrown: Unable to load asset: assets/images/Soundboard Max.png      

我的实现如下所示:

class SoundboardGrid extends StatelessWidget {
  SoundboardGrid({
    Key key,
    @required this.sounds,
  }) : super(key: key);

  final List<Sound> sounds;
  final Player audioPlayer = Player();

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/background.png'),
          fit: BoxFit.cover,
        ),
      ),
      child: GridView.count(
        scrollDirection: Axis.vertical,
        crossAxisCount: 4,
        childAspectRatio: 0.8,
        children: List.generate(
          sounds.length,
          (index) {
            return Column(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Align(
                  alignment: Alignment.center,
                  child: GestureDetector(
                    onTap: () {
                      audioPlayer.play(sounds[index].filename);
                    },
                                      child: Container(
                      child: Padding(
                        padding: EdgeInsets.fromLTRB(12, 12, 12, 3),
                        child: Image.asset('assets/images/Soundboard ${sounds[index].speaker}.png')
                      ),
                    ),
                  ),
                ),
                Container(
                  child: Text(
                    sounds[index].filename,
                    style: TextStyle(color: Colors.white, fontSize: 11),
                    textAlign: TextAlign.center,
                  ),
                  alignment: Alignment.bottomCenter,
                )
              ],
            );
          },
        ),
      ),
    );
  }
}

使用 Imageprovider 而不是“Image.asset Widget”会得到相同的结果。

【问题讨论】:

  • 所有丢失的图标文件的名称中是否都包含空格?如果是这样,您能否将其删除或替换为下划线,然后再试一次?
  • 就是这样!非常感谢!

标签: android flutter build widget assets


【解决方案1】:

尝试在 pubspec.yaml 文件中导入单个资产。这可能有效。 将其放在其他资产下方的资产文件中。

- assets/images/Soundboard Max.png

此解决方案可能有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-24
    • 2020-03-03
    • 2020-02-20
    • 2019-10-24
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多