Osama Aldawoody
Osama Aldawoody

Reputation: 21

'Expected a key while parsing a block mapping' in the pubspec.yaml

i am beginner in flutter.

i want to use asset image in my project, i created folder named assets and pasted the image in it.

i just followed the instructor steps and i modified the pubspec file.

the file was as this:

uses-material-design: true

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

i just uncommented and modified to my image path as this :

uses-material-design: true

  # To add assets to your application, add an assets section, like this:
   assets:
    - assets/type1.png

when i pushed run button this error happened to me :

Error detected in pubspec.yaml:

Error on line 44, column 4: Expected a key while parsing a block mapping.

44 │ assets:

│ ^

note : this was the first modification in the pubspec file.

Upvotes: 2

Views: 4091

Answers (4)

Sudhir Chovatiya
Sudhir Chovatiya

Reputation: 61

  # 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/

Upvotes: 0

Johnett Mathew
Johnett Mathew

Reputation: 327

This is a combined version of both @chunhunghan and @Kushagra Saxena Answers(for better understanding)

When you first try to add the asset folder to the pubspec.yaml file there are two options.

  1. Uncomment the existing code and edit it the way you wanted.

  2. Delete or keep the commented asset section and add a new one yourself.

Initially the section(asset section of pubspec.yaml file) looks like below.

   42   # The following line ensures that the Material Icons font is
   43   # included with your application, so that you can use the icons in
   44   # the material Icons class.
   45   uses-material-design: true
   46 
   47   # To add assets to your application, add an assets section, like this:
   48   # assets:
   49   #   - images/a_dot_burr.jpeg
   50   #   - images/a_dot_ham.jpeg

following the first approach(please see above 2 methods) uncommenting line 48 - 50 won't bring the assets(line 48) aligned with uses-material-design: true(line 45) block.

if you follow the second approach there is a great chance you are going to mess up the alignment.

For me I trusted the reformat code option(which available in both VS code and Android Studio) and it's failed me. So I had to correct the alignment my-self.

The correct code will look like below.

   42  # The following line ensures that the Material Icons font is
   43  # included with your application, so that you can use the icons in
   44  # the material Icons class.
   45  uses-material-design: true
   46
   47  # To add assets to your application, add an assets section, like this:
   48  assets:
   49    - assets/images/

Now the keys are aligned(line 45(uses-material-design: true) and 48(assets:)) and the error will be gone.

Upvotes: 0

chunhunghan
chunhunghan

Reputation: 54407

space and indent is meaningful in YAML

  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
  assets:
    - assets/images/

Upvotes: 3

Kushagra Saxena
Kushagra Saxena

Reputation: 669

Your assets needs to be aligned with the key uses-material-design, .yaml files work on indentation of the code inside them after properly indenting the code I think it should work.

Upvotes: 2

Related Questions