簡體   English   中英

Flutter 無法在物理設備上加載圖像資產(但在模擬器上加載它就好了)

[英]Flutter unable to load image asset on physical device (but loading it just fine on the emulator)

我正在構建一個 flutter 應用程序,當我在手機上運行該應用程序時不會加載資產圖像,但是當我在模擬器上運行該應用程序時它可以正常加載。

這是我得到的錯誤:

> I/flutter (26364): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE
> ╞════════════════════════════════════════════════════ I/flutter
> (26364): The following assertion was thrown resolving an image codec:
> I/flutter (26364): Unable to load asset: images/diamond.png I/flutter
> (26364): I/flutter (26364): When the exception was thrown, this was
> the stack: I/flutter (26364): #0      PlatformAssetBundle.load
> (package:flutter/src/services/asset_bundle.dart:221:7) I/flutter
> (26364): <asynchronous suspension> I/flutter (26364): #1     
> AssetBundleImageProvider._loadAsync
> (package:flutter/src/painting/image_provider.dart:464:44) I/flutter
> (26364): <asynchronous suspension> I/flutter (26364): #2     
> AssetBundleImageProvider.load
> (package:flutter/src/painting/image_provider.dart:449:14) I/flutter
> (26364): #3      ImageProvider.resolve.<anonymous closure>.<anonymous
> closure>.<anonymous closure>
> (package:flutter/src/painting/image_provider.dart:316:48) I/flutter
> (26364): #4      ImageCache.putIfAbsent
> (package:flutter/src/painting/image_cache.dart:160:22) I/flutter
> (26364): #5      ImageProvider.resolve.<anonymous closure>.<anonymous
> closure> (package:flutter/src/painting/image_provider.dart:316:25)
> I/flutter (26364): (elided 13 frames from package dart:async)
> I/flutter (26364): I/flutter (26364): Image provider:
> AssetImage(bundle: null, name: "images/diamond.png") I/flutter
> (26364): Image key: AssetBundleImageKey(bundle:
> PlatformAssetBundle#5b025(), name: "images/diamond.png", I/flutter
> (26364):   scale: 1.0) I/flutter (26364):
> ════════════════════════════════════════════════════════════════════════════════════════════════════

這是我的 pubspec.yaml

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

這里是main.dart:

導入“包:顫振/material.dart”;

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.blueGrey[900],
          title: Text('Picture Motivation'),
        ),
        body: Center(
          child: Image(
            image: AssetImage('images/diamond.png'),
          ),
        ),
      ),
    ),
  );
}

非常令人困惑的是為什么圖像在模擬器上正確加載而不是在物理設備上。 有什么線索嗎?

非常感謝。

逐步執行此操作,這應該可以正常工作。 基本上你做錯了。

  1. 將圖片放在 assets 文件夾中,所以根目錄是assets/diamond.png
  2. 或者,如果您的 assets 文件夾中有另一個文件夾images ,那么根目錄是assets/images/diamond.png

pubspec.yaml

flutter:
   uses-material-design: true
   //you have to give the proper path in order to get the image in your UI
   assets: 
     - assets/images/diamond.png

用戶界面代碼:

//this is how your image calling work, you just have to copy the path, same as pubspec.yaml
Center(
   child: Image(
      image: AssetImage('assets/images/diamond.png'),
   )
)

我相信你現在就能得到圖像。 讓我知道這是否有幫助。

暫無
暫無

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

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