Flutter 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:
Incorrect Indentation
YAML relies on spaces (never tabs!) to define hierarchy. Yourassetsfield must be a child of theflutterkey, aligned with other flutter-specific settings likeuses-material-design. Here's the correct structure:flutter: uses-material-design: true assets: - assets/your_image.png - assets/your_data.jsonMake sure
assets:is indented with 2 spaces underflutter:(same asuses-material-design:), and each asset path is indented with another 2 spaces underassets:.Missing Space After Colon
Double-check thatassets:has a space after the colon. If you wroteassets:without a space immediately after, YAML will fail to recognize it as a valid key.Incomplete
dev_dependenciesSection
Your provided code cuts off atdev_depe...—if this section isn't properly closed or formatted, it can break the entire YAML file. Make sure yourdev_dependenciesare fully written out, like this:dev_dependencies: flutter_test: sdk: flutterMalformed 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

