【问题标题】:'Expected a key while parsing a block mapping' in the pubspec.yamlpubspec.yaml 中的“解析块映射时需要一个键”
【发布时间】:2020-01-24 06:06:15
【问题描述】:

我是 Flutter 的初学者。

我想在我的项目中使用资产图像,我创建了名为 assets 并将图像粘贴到其中。

我只是按照讲师的步骤修改了 pubspec 文件。

文件是这样的:

uses-material-design: true

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

我只是取消注释并修改为我的图像路径:

uses-material-design: true

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

当我按下运行按钮时,这个错误发生在我身上:

在 pubspec.yaml 中检测到错误:

第 44 行第 4 列出错:解析块时需要一个键 映射。
╷

44 │ 资产:

│^

╵

注意: 这是 pubspec 文件中的第一次修改。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您的assets 需要与键uses-material-design 对齐,.yaml 文件在正确缩进我认为应该可以工作的代码后对其中的代码进行缩进。

    【讨论】:

    • 很高兴能帮上忙!
    【解决方案2】:

    空格和缩进在 YAML 中是有意义的

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

    【讨论】:

      【解决方案3】:

      这是 @chunhunghan 和 @Kushagra Saxena 答案的组合版本(以便更好地理解)

      当您第一次尝试将资产文件夹添加到 pubspec.yaml 文件时,有两个选项。

      1. 取消注释现有代码并按照您想要的方式对其进行编辑。

      2. 删除或保留已评论的资产部分并自己添加一个新部分。

      最初部分(pubspec.yaml 文件的资产部分)如下所示。

         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
      

      按照第一种方法(请参阅上述两种方法)取消注释第 48 - 50 行不会使assets(第 48 行)与uses-material-design: true(第 45 行) 块。

      如果您采用第二种方法,则很有可能会弄乱对齐方式。

      对我来说,我相信重新格式化代码选项(在 VS 代码和 Android Studio 中都可用),但它让我失望了。所以我不得不自己纠正对齐方式。

      正确的代码如下所示。

         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/
      

      现在键已对齐(第 45 行(uses-material-design: true)和 48(assets:)),错误将消失。

      【讨论】:

        【解决方案4】:
          # 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/
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-04
          • 1970-01-01
          • 2019-08-23
          • 1970-01-01
          • 2018-10-14
          • 2016-11-11
          • 2020-03-21
          相关资源
          最近更新 更多