解决Flutter错误:无法加载资产

时间:2020-10-08 02:55:16

标签: android-studio flutter dart assets

我已经尝试了一切,包括对pubspec.yaml文件的每次修改,清理干净,更新清理和我的IDE(Android Studio)。似乎什么都没用。

在图像显示的页面上没有交易的情况下构建货币跟踪器应用程序。

图像文件的路径:

lib/assets/images/waiting.png

代码:

return Container(
      height: 300,
      child: transactions.isEmpty
          ? Column(
              children: <Widget>[
                Text(
                  'No Transactions yet',
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0,
                  ),
                ),
                Container(
                  height: 200,
                  child: Image.asset(
                    'assets/images/waiting.png',
                    fit: BoxFit.cover,
                  ),
                ),
              ],
            )
          : ListView.builder(
              itemBuilder: (context, index) {
                return Container(
                  margin: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
                  padding: EdgeInsets.all(3.0),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(colors: _txColors),
                    border: Border.all(color: Colors.grey, width: 2.0),
                    borderRadius: BorderRadius.all(
                      Radius.circular(15.0),
                    ),
                    color: Colors.greenAccent,
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      Container(
                        child: Text(
                          '\$${transactions[index].amount.toStringAsFixed(2)}',
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 20.0,
                            color: Colors.white,
//                    Color(0xffC5CED4),
                          ),
                        ),
                      ),

Pubspec.yaml:

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

错误:

I/flutter ( 5679): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 5679): The following assertion was thrown resolving an image codec:
I/flutter ( 5679): Unable to load asset: assets/images/waiting.png
I/flutter ( 5679): 
I/flutter ( 5679): When the exception was thrown, this was the stack:
I/flutter ( 5679): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter ( 5679): <asynchronous suspension>
I/flutter ( 5679): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:664:31)
I/flutter ( 5679): #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:648:14)
I/flutter ( 5679): #3      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:501:13)
I/flutter ( 5679): #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:359:22)
I/flutter ( 5679): #5      ImageProvider.resolveStreamForKey (package:flutter/src/painting/image_provider.dart:499:80)
I/flutter ( 5679): #6      ScrollAwareImageProvider.resolveStreamForKey (package:flutter/src/widgets/scroll_aware_image_provider.dart:106:19)
I/flutter ( 5679): #7      ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:330:9)
I/flutter ( 5679): #8      ImageProvider._createErrorHandlerAndKey.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:460:26)
I/flutter ( 5679): (elided 13 frames from dart:async)
I/flutter ( 5679): 
I/flutter ( 5679): Image provider: AssetImage(bundle: null, name: "assets/images/waiting.png")
I/flutter ( 5679): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#288b8(), name:
I/flutter ( 5679):   "assets/images/waiting.png", scale: 1.0)
I/flutter ( 5679): ════════════════════════════════════════════════════════════════════════════════════════════════════

3 个答案:

答案 0 :(得分:1)

如Omi Shah所说,将资产文件夹移到lib文件夹的同一级别。

请尝试更改pubspec.yaml和资产网址'

Image.asset(
                    'lib/assets/images/waiting.png',
                    fit: BoxFit.cover,
                  ),
flutter:
uses-material-design: true
assets:
  - lib/assets/images/

答案 1 :(得分:0)

我确实必须将资产文件夹移动到与lib文件夹相同的级别。但是我还必须解决pubspec.yaml文件的缩进问题。

flutter:
  uses-material-design: true
  assets:
   - assets/
--------------------------------
flutter:
[space][space]uses-material-design: true
[space][space]assets:
[space][space][space]- assets/

答案 2 :(得分:0)

在这上面花了几分钟的时间,一切看起来都不错,识别、路径等。 诀窍是从终端运行flutter clean并重建。