Flutter Pub: Expected a key while parsing a block mapping. path:
AndroidPathMappingFlutterDart PubAndroid Problem Overview
I'm using JSON file and register in Pubspec.ymal
but showing error and also when I use an image and register it, the same error also occurs. Maybe there is a formatting mistake in it, but don't know what the problem is.this is the doc I followed.
> Error on line 29, column 5 of pubspec.yaml: Expected a key while parsing a block mapping. > assets: > ^
This is my pubspec.yaml
file
name: jsondata description: A new Flutter application.
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- loadjson/person.json
Android Solutions
Solution 1 - Android
Spaces are significant in YAML
assets
is indented too far
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- loadjson/person.json
Solution 2 - Android
It is because of the indentation of your code.The uses-material-design
should be on the same line vertically as the assets:
.
uses-material-design: true
assets:
- images/picture.png
Solution 3 - Android
> Error on line 29, column 4: Expected a key while parsing a block > mapping. assets: > > flutter: uses-material-design: true assets: > - assets/images/image.jpg
In my case i just added #
in front of uses-material-design: true
Try below code
flutter:
# uses-material-design: true
assets:
- assets/images/image.jpg
its working fine.
Solution 4 - Android
In my case, uses-material-design
indentation was broken. I put 1 (one) more space before it, and it worked fine.
Solution 5 - Android
With removed comments, original, generated by default pubspec.yaml looks like this
flutter:
uses-material-design: true
assets:
- images/abc.jpg
But it isn't vaild. It doesn't works. It should be:
flutter:
uses-material-design: true
assets:
- images/abc.jpg
Important: Spaces are significant in YAML
Solution 6 - Android
Spaces are very important in .yaml file.
Just make sure your code is on the same line (vertically) according to key and value.
Or
If you are still confused please watch this simple video. It'll help you to solve the issue easily. > https://imgur.com/gallery/ngoXwUe
Solution 7 - Android
**Error on line 46, column 4: Expected a key while parsing a block mapping. assets:
flutter: uses-material-design: true assets: - assets/dhaka.jpg**
Make sure when you uncomment the line please check indentation. if indentation is wrong it's going error. flowing this image. Happy coding !!!
Solution 8 - Android
You have to be careful with space in yml file
Solution 9 - Android
If images
folder is next to lib
folder, you need a ./
, because it is not on the same level as the pubspec.yaml
, then, in pubspec.yaml
uncomment:
assets:
- ./images/ //Watch for spaces and indentation!!
Now you can use them:
Image.asset(
'../images/nature.png',
width: 600,
height: 240,
fit: BoxFit.cover,
),
Solution 10 - Android
Get same problem after uncommenting assets
and image link sample
in pubspec.yaml.
If you faced problem with assets
, like I was. Make sure that it starts with same column(starts with same indent as pre-defined property like uses-material-design
). After uncommenting in my case, their location was different.
After that, the problem was solved.
Solution 11 - Android
In My Case, the image was too large to load, decreasing dimension of image worked.
Solution 12 - Android
Please make sure that we don't have unwanted whitespace since yaml structure is sensitively considering the white spaces. Please refer the documentation for more info as below link,
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
Solution 13 - Android
As @Baftek
mentioned , just indent before uses-material-design:true and press Ctrl+Alt+l
(Intellij or Android Studio) to reformat file.
Solution 14 - Android
Issue is related with indent spacing, check your spacing, it should work fine
fonts:
- family: CM Sans Serif
fonts:
- asset: assets/fonts/cm_sans_serif_2012.ttf
Solution 15 - Android
Assets should be on the same line as uses-material-design:
uses-material-design: true
assets:
- images/myimage.jpg
Indentation Rules for pubspec.yaml
-
uses-material-design = 2 spaces
-
assets = 2 spaces
-
-images/ = 4 spaces
-
fonts: = 2 spaces
-
-family: = 4 spaces
-
fonts: = 6 spaces
-
-asset: = 8 spaces
-