图片未加载到 Flutter 中

Image not loading in Flutter

尝试在 flutter 中加载图像但无法执行此操作,出现以下错误

I/flutter ( 3214): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════ I/flutter ( 3214): The following assertion was thrown resolving an image codec: I/flutter ( 3214): Unable to load asset: assets/test.jpg I/flutter ( 3214): I/flutter ( 3214): When the exception was thrown, this was the stack: I/flutter ( 3214): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7) I/flutter ( 3214): I/flutter ( 3214): #1
AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:429:44) I/flutter ( 3214): I/flutter ( 3214): #2
AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:414:14) I/flutter ( 3214): #3 ImageProvider.resolve.. (package:flutter/src/painting/image_provider.dart:267:86) I/flutter ( 3214): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:143:20) I/flutter ( 3214): #5 ImageProvider.resolve. (package:flutter/src/painting/image_provider.dart:267:63) I/flutter ( 3214): (elided 8 frames from package dart:async) I/flutter ( 3214): I/flutter ( 3214): Image provider: AssetImage(bundle: null, name: "assets/test.jpg") I/flutter ( 3214): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#81de5(), name: "assets/test.jpg", scale: I/flutter ( 3214): 1.0) I/flutter ( 3214):

main.dart

lass MyApp extends StatelessWidget {
Widget build(BuildContext context) {
    return MaterialApp(
    home: Scaffold(
    appBar: AppBar(
      title: Text('EasyList'),
    ),
    body: Card(child: Column(children: < Widget > [
      Image.asset('assets/test.jpg'),
    ],),),
   ),
  );
 }
}

pubspec.yaml

flutter:
  uses-material-design: true

assets:
  - assets/test.jpg

单词 assets: 前需要两个空格,破折号前需要两个空格(总共 4 个)。

flutter:
  uses-material-design: true

  assets:
    - assets/test.jpg

或者,如果您使用的是 Android Studio IDE,那么在您的 pubspec.yaml 文件中,只需按 CTRL+ALT+L 重新排列代码。然后点击 packages get .