質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

5172閲覧

Flutter: アセットから画像を取得できない

massanmesu

総合スコア36

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/06/22 09:32

プロジェクト内にimagesフォルダを作成し、その中にpng形式の画像が四枚入ってます。
イメージ説明

pubspec.yamlで以下のように設定し、imagesフォルダの中の画像を取得しようとしています。
イメージ説明

main.dartからImage.asset()で画像を呼ぶような処理を書いて実行したところ、取得が失敗する旨のエラーが発生しました。

import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const GridViewCount()); } } class GridViewCount extends StatelessWidget { const GridViewCount({Key? key}) : super(key: key); @override Widget build(BuildContext context) { var list = [ _photoItem("icon0"), _photoItem("icon1"), _photoItem("icon2"), _photoItem("icon3"), ]; return Scaffold( appBar: AppBar( title: const Text("GridView"), ), body: GridView.count( crossAxisCount: 2, children: list, ), ); } Widget _photoItem(String image) { // ファイルパス var assetsImage = "images/" + image + ".png"; return Container( child: Image.asset( // 画像呼び出し assetsImage, fit: BoxFit.cover, ), ); } }

エラー内容

======== Exception caught by image resource service ================================================ The following assertion was thrown resolving an image codec: Unable to load asset: images/icon0.png When the exception was thrown, this was the stack: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7) <asynchronous suspension> #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14) <asynchronous suspension> Image provider: AssetImage(bundle: null, name: "images/icon0.png") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#4ba6d(), name: "images/icon0.png", scale: 1.0) ==================================================================================================== ======== Exception caught by image resource service ================================================ The following assertion was thrown resolving an image codec: Unable to load asset: images/icon1.png When the exception was thrown, this was the stack: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7) <asynchronous suspension> #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14) <asynchronous suspension> Image provider: AssetImage(bundle: null, name: "images/icon1.png") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#4ba6d(), name: "images/icon1.png", scale: 1.0) ==================================================================================================== ======== Exception caught by image resource service ================================================ The following assertion was thrown resolving an image codec: Unable to load asset: images/icon2.png When the exception was thrown, this was the stack: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7) <asynchronous suspension> #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14) <asynchronous suspension> Image provider: AssetImage(bundle: null, name: "images/icon2.png") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#4ba6d(), name: "images/icon2.png", scale: 1.0) ==================================================================================================== ======== Exception caught by image resource service ================================================ The following assertion was thrown resolving an image codec: Unable to load asset: images/icon3.png When the exception was thrown, this was the stack: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7) <asynchronous suspension> #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14) <asynchronous suspension> Image provider: AssetImage(bundle: null, name: "images/icon3.png") Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#4ba6d(), name: "images/icon3.png", scale: 1.0) ====================================================================================================

Flutter初心者のため、初歩的なところでミスしている気もしますが、特定できませんでした。

ご教授お願いします。

参考記事
https://qiita.com/yu124choco/items/a2710ec004d3425a2a0b

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

普段、エミュレータで試していたのですが、実機に変えたら表示されました。
理由はわかりませんが、以降は実機で動作確認することにします。

投稿2021/06/22 13:37

massanmesu

総合スコア36

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

インデントブロックを正しく考慮して記述していますか。
pubspec.yamlではインデントも含めたパース(コードの解析)が行われるので

assets: - images/

のように、- images/の前に半角スペースを2つ(環境によっては4つ)挿入してみてください。
それでもUnable to load assetのエラーが出る場合は、そもそもassetsを記述する位置が違うのかもしれません。
参考

投稿2021/06/22 10:26

tmsah

総合スコア101

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

massanmesu

2021/06/22 11:58

ご回答ありがとうございます。 半角スペースを2つ4つとも試しましたが変化ありませんでした。 記述場所も`flutter:`インデントブロックの中です。 今やりたいことはGridViewの使い方を理解することなので、最悪表示されなくても挙動の確認はできますので、今のところは諦めて作業に集中します。 ご教授ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問