-1

Hello I am trying to create a list item in flutter. I have created layout like this but image is not displaying.

Does anyone know whats reason of it ?

import 'package:flutter/material.dart';

class CommonListItem extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _CommonListItemState();
}

class _CommonListItemState extends State<CommonListItem> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: Container(
        child: new Column(
          children: <Widget>[
            Container(
              height: 100.0,
              width: 100.0,
              child: new Image(image: AssetImage("assets/images/mountains.jpg")),
            )
          ]
        ),
      ),
    );
  }
}

pubspec.yml

name: xx
description: xx

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: 0.7.4
  firebase_auth: 0.5.18



  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
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/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.io/custom-fonts/#from-packages
N Sharma
  • 33,489
  • 95
  • 256
  • 444

5 Answers5

3
new Image(image: AssetImage("assets/images/mountains.jpg")),

With the code above make sure you have created the directories (case sensitive!) in the root of the application e.g c:/myapp/assets/images/

Then in your pubspec file you need to un-comment that line pointing to the assets

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

You don't have to list every item in the folder, use the asterisk to include all files.

F-1
  • 2,887
  • 20
  • 28
  • It is working fine in another file with same pubspec, I have this in another file `Container( height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.white, image: DecorationImage( colorFilter: new ColorFilter.mode( Colors.black.withOpacity(0.05), BlendMode.dstATop), image: AssetImage('assets/images/mountains.jpg'), fit: BoxFit.cover, ), )` – N Sharma Nov 03 '18 at 05:37
  • Issue is something in my layout, which I could not find. Any idea ? – N Sharma Nov 03 '18 at 05:45
  • I don't have this image in my asset `assets/images/mountains.jpg` it is loading in another screen but not above, I think it is inbuilt image of flutter – N Sharma Nov 03 '18 at 06:16
1

To use images from assets:

Image.asset("assets/images/source.jpg")

Just make sure you also have the asset set up in your pubspec.yaml.

soupjake
  • 3,293
  • 3
  • 17
  • 32
  • 1
    image is loading correct from assets in another file but not in my above code – N Sharma Nov 02 '18 at 11:52
  • You're not adding the image assets in your pubspec.yaml. It tells you have to do so within it `# To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg ` – soupjake Nov 02 '18 at 11:54
  • I don't have this image in my asset `assets/images/mountains.jpg` it is loading in another screen but not above, I think it is inbuilt image of flutter – N Sharma Nov 03 '18 at 06:19
1

Try to do this in your pubsec.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
  assets:
    - assets/

And after that use assets

Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105
  • It is working fine in another file with same pubspec, I have this in another file `Container( height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.white, image: DecorationImage( colorFilter: new ColorFilter.mode( Colors.black.withOpacity(0.05), BlendMode.dstATop), image: AssetImage('assets/images/mountains.jpg'), fit: BoxFit.cover, ), )` – N Sharma Nov 03 '18 at 05:38
  • Issue is something in my layout, which I could not find. Any idea ? – N Sharma Nov 03 '18 at 05:45
  • I don't have this image in my asset `assets/images/mountains.jpg` it is loading in another screen but not above, I think it is inbuilt image of flutter – N Sharma Nov 03 '18 at 06:17
1

You introduce wrong url from assets , if your folder is called images your url is : images/mountains.jpg so you nee to change code :

 new Image(image: AssetImage("images/mountains.jpg"))
Roger Cuesta
  • 902
  • 1
  • 8
  • 12
  • It is working fine in another file with same pubspec, I have this in another file `Container( height: MediaQuery.of(context).size.height, decoration: BoxDecoration( color: Colors.white, image: DecorationImage( colorFilter: new ColorFilter.mode( Colors.black.withOpacity(0.05), BlendMode.dstATop), image: AssetImage('assets/images/mountains.jpg'), fit: BoxFit.cover, ), )` – N Sharma Nov 03 '18 at 06:19
  • Issue is something in my layout, which I could not find. Any idea ? – N Sharma Nov 03 '18 at 06:19
  • I don't have this image in my asset `assets/images/mountains.jpg` it is loading in another screen but not above, I think it is inbuilt image of flutter – N Sharma Nov 03 '18 at 06:19
1

Be careful about tabs in Flutter. have a look at the belowing link.

FlutterError: Unable to load asset