簡體   English   中英

無法加載資產 [flutter]

[英]unable to load asset [flutter]

我昨天嘗試放置一個圖像並且它工作得很好但是當我再次嘗試運行它時它給了我這個錯誤:

Restarted application in ٢٬٧٦٥ms.

[38;5;248m════════ Exception caught by image resource service ════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown resolving an image codec:[39;49m
Unable to load asset: cat.jpg

[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      PlatformAssetBundle.load[39;49m
[38;5;244m<asynchronous suspension>[39;49m
[38;5;244m#1      AssetBundleImageProvider._loadAsync[39;49m
[38;5;244m<asynchronous suspension>[39;49m
[38;5;244m#2      AssetBundleImageProvider.load[39;49m
[38;5;244m...[39;49m
[38;5;244mImage provider: AssetImage(bundle: null, name: "cat.jpg")[39;49m
[38;5;244mImage key: AssetBundleImageKey(bundle: PlatformAssetBundle#7533e(), name: "cat.jpg", scale: 1.0)[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

這是yaml:

flutter:
  uses-material-design: true
  assets:
    - images/cat.jpg

並在主要

child: Image(
  image: AssetImage('cat.jpg'),
),

那么這里有什么問題呢?

嘗試更改此代碼:

child: Image(
   image: AssetImage('cat.jpg'),
),

進入這個:

// add the full reference of the image
child: Image(
   image: AssetImage('images/cat.jpg'),
),

如果這對您不起作用,請嘗試將您的圖像文件夾移動到 lib 文件夾中並像這樣更改您的引用:

在 yaml 中:

flutter:
   uses-material-design: true
   assets:
      - lib/images/cat.jpg

在你的飛鏢代碼中:

//the full reference of the image
child: Image(
   image: AssetImage('lib/images/cat.jpg'),
),

您必須像在pubspec.yaml 中聲明的那樣提供圖像小部件的完整路徑

child: Image(
  image: AssetImage('images/cat.jpg'),
),

附加您的圖像文件在您的應用程序頂級目錄中,如下所示

->(Your_App_Name)
  ->images
   ->cat.jpg

在主文件中添加完整路徑。

Image(
   image: AssetImage('images/cat.jpg'),
),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM