You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Pubspec.yaml解析报错:预期键值,资产配置问题求助

Fixing "Expected a key while parsing a block mapping" in pubspec.yaml

Hey there! That YAML parsing error you're hitting is super common when configuring assets in Flutter's pubspec.yaml—YAML is extremely picky about indentation and syntax, so even a missing space or wrong tab can throw things off. Let's walk through the most likely issues and fix them.

Common Causes & Fixes

The error points to line 29 where your assets field lives, so let's focus on that area first:

  1. Incorrect Indentation
    YAML relies on spaces (never tabs!) to define hierarchy. Your assets field must be a child of the flutter key, aligned with other flutter-specific settings like uses-material-design. Here's the correct structure:

    flutter:
      uses-material-design: true
      assets:
        - assets/your_image.png
        - assets/your_data.json
    

    Make sure assets: is indented with 2 spaces under flutter: (same as uses-material-design:), and each asset path is indented with another 2 spaces under assets:.

  2. Missing Space After Colon
    Double-check that assets: has a space after the colon. If you wrote assets: without a space immediately after, YAML will fail to recognize it as a valid key.

  3. Incomplete dev_dependencies Section
    Your provided code cuts off at dev_depe...—if this section isn't properly closed or formatted, it can break the entire YAML file. Make sure your dev_dependencies are fully written out, like this:

    dev_dependencies:
      flutter_test:
        sdk: flutter
    
  4. Malformed Asset List Items
    Each asset path must start with - (dash followed by a space). If you omitted the space (e.g., -assets/file.json) or used a different character, YAML won't parse the list correctly.

Full Corrected Example

Here's how your complete pubspec.yaml section should look, fixing all potential issues:

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:
    - assets/images/sample_image.png
    - assets/data/sample_json.json

Just adjust the asset paths to match your project's structure, and make sure all indentation uses spaces (not tabs). That should resolve the parsing error!

内容的提问来源于stack exchange,提问作者Farhana Naaz Ansari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:02:07