Flutter Pub: 解析块映射时需要一个键:

我在 Pubspec.ymal中使用 JSON 文件和寄存器,但显示错误,而且当我使用图像并注册它时,也会发生同样的错误。也许有一个格式错误,但不知道是什么问题。这是我跟随的 医生

在 pubspec.yaml 的第29行第5列出错: 在解析块映射 时期望获得一个键。 资产: ^

这是我的 pubspec.yaml文件

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

enter image description here

118879 次浏览

Spaces are significant in YAML

assets is indented too far

flutter_test:
sdk: flutter


flutter:
uses-material-design: true
assets:
- loadjson/person.json

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.

In My Case, the image was too large to load, decreasing dimension of image worked.

In my case, uses-material-design indentation was broken. I put 1 (one) more space before it, and it worked fine.

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

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

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

As @Baftek mentioned , just indent before uses-material-design:true and press Ctrl+Alt+l (Intellij or Android Studio) to reformat file.

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

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

**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 !!!

enter image description here

You have to be careful with space in yml file

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,
),

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.

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