raluca2
raluca2

Reputation: 43

Flutter image from asset

I am trying to do an mobile app in flutter and everytime I try to add a photo I get an error like: " Failed assertion line 134 pos 15: 'assetName != null':is not true. I observe that I didn't have the asset folder so I created it and add photos png in it. It's the problem with the size of the photos? or what?I also uncomment that #assetcode line from pubspec.yamlerror photo

assets:
- assets/burger.png

import '../models/category_model.dart';

final categories = [
  Category(
    numberOfItems: 57,
    imagePath: "assets/burger.png",
    categoryName: "Legume"
  ),
category model
class Category{
final String categoryName;
final String imagePath;
final int numberOfItems;

Category({this.categoryName,this.imagePath,this.numberOfItems});

}
enter image description here

Upvotes: 1

Views: 726

Answers (2)

Ashesh Shrestha
Ashesh Shrestha

Reputation: 388

Make sure that pubspec.yaml has corrent indentation.

  assets:      // two spaces before assets. 
  - assets/     // two spaces before - and a space after -. This assets is for folder name. 

First assets is for system. It remains constant. The second assets is your folder name. It depends based on your folder name.

If you want to access just a subfolder images inside assets folder,

  - assets/images/
//or
  - assets/images/burger.png

Hope this works for you.

Upvotes: 0

kaique_munhoz
kaique_munhoz

Reputation: 31

The indentation on the file pubspec.yaml is very important to import the packages or files correctly.

To import your image, you have to put more two spaces in - assets/burger.png

Try:

 assets:
    - assets/burger.png

If you need to import all the images in this folder you could do:

 assets:
    - assets/

For more information, consult the documentation: Flutter - Assets And Images

Upvotes: 1

Related Questions