user12711663
user12711663

Reputation: 98

Flutter : image assets are not showing in android build

I am trying a simple flutter app. It is working fine on web. But on android, images can't load.

this is my pubspec.yaml

flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - images/
    - gif/

I have images and gif folders at root of project.

and I am loading images like this

child: Image(image: AssetImage("/images/abc.jpg"),
or 
child: Image(image: AssetImage("/gif/abc.gif"),

This works fine when I run on IDE, but on making android apk, images and gif are not showing.

In android folder, there is no assets directory as well. I have tired flutter clean and flutter create . but no luck.

Upvotes: 0

Views: 1495

Answers (3)

Mandeep Singh Tanwar
Mandeep Singh Tanwar

Reputation: 181

  1. make sure you declare the assets in pubspec.yml e.g.
flutter:
  uses-material-design: true
  generate: true

  assets:
    - assets/data/chapters_data.json
  1. Loading of assets for webapp or windows app works even if you don't include assets/ in path, but it won't work for Android. e.g.
    String data = await rootBundle.loadString('data/chapters_data.json'); // <-- this won't work for android
    List<dynamic> chapters = jsonDecode(data);

To make this work for all the platforms (including Android) use assets/ as prefix. e.g.

    String data = await rootBundle.loadString('assets/data/chapters_data.json');
    List<dynamic> chapters = jsonDecode(data);

Note that at both places (pubspec.yml and flutter code) the path includes assets/

Upvotes: 0

You also need to add following code to AndroidManifest.xml file located at android/app/src/main inside the manifest tag: <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 0

Yunus Emre &#199;elik
Yunus Emre &#199;elik

Reputation: 326

You need to use like this

Image(image: AssetImage("images/abc.jpg")),

Upvotes: 2

Related Questions