LoginSignup
5
3

More than 3 years have passed since last update.

FlutterでSDKインストールの際にエラー Expected a key while parsing a block mapping.

Posted at

はじめに

モバイルアプリ開発初心者でFlutter学習中です。自分用メモとしても残しております。
Flutterでフォルダ内の画像を表示しようとpubspec.yamlを編集していました。

Flutterの画像表示方法はこちらのqiita記事が大変参考になりました。

エラーでパッケージ取得できない

assetsを追加してパッケージを取得しようとしたら以下のエラーが発生

Error detected in pubspec.yaml:
Error on line 45, column 4: Expected a key while parsing a block mapping.
   ╷
45 │    assets:
   │    ^
   ╵
Please correct the pubspec.yaml file at pubspec.yaml
Process finished with exit code 1

assetsが間違ってるっぽいけど、タイポでもなさそうなのでどうしてだろうと悩んでいたのですが、
エラーログをよく見るとcolumnって書かれていることに気がつきました。
なんで行だけじゃなく、列についても指摘されてるんだろう?

原因

こちらの記事が大変参考になりました。

原因はYAMLの文法エラーでした。
YAMLはインデントでデータ構造を表現するようで、今回はインデントが崩れていてエラーが出ていました。そのためエラーログではcolumnについても指摘してくれていたのですね。

新規Flutterプロジェクト作成で生成されるpubspec.yamlではassetsが自動でコメントアウトされており、当初はコメントアウトを外しただけでした。
その場合インデントが崩れている状態になってしまっていたので、揃えてあげる必要がありました。

NG(単純にコメントアウトを外したもの)

pubspec.yaml
# 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

OK(インデント揃えたもの)

pubspec.yaml
# 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

こんな簡単なエラーだったとは!
でもおかげでYAMLも少し知れて、いい勉強になりました!

参考

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3