如何修复Flutter 3.7.0中的「ICU Syntax Error: Expected "identifier" but found "0"」错误
解决Flutter 3.7.0升级后ARB文件ICU语法错误的方法
问题根源
Flutter 3.7.0绑定的intl包版本更新后,对ARB文件的ICU消息格式语法校验更严格,不再允许使用数字索引(如{0})作为占位符,强制要求使用命名标识符作为参数占位符。
具体修复步骤
修改ARB文件占位符
将数字索引占位符替换为命名参数,示例如下:
错误写法:"scanCode_fieldNotMatched": "field to match is {0}", "@scanCode_fieldNotMatched": { "placeholders": { "0": {} } }正确写法:
"scanCode_fieldNotMatched": "field to match is {targetField}", "@scanCode_fieldNotMatched": { "placeholders": { "targetField": {} } }同步更新Dart代码调用
修改代码中调用该本地化字符串的逻辑,将位置参数改为命名参数:
错误写法:AppLocalizations.of(context)!.scanCode_fieldNotMatched("phone");正确写法:
AppLocalizations.of(context)!.scanCode_fieldNotMatched(targetField: "phone");重新生成本地化代码
执行项目原有的本地化代码生成命令(如flutter gen-l10n或flutter pub run intl_utils:generate),确保新的命名参数逻辑被正确生成到AppLocalizations类中。
临时过渡方案(不推荐)
如果需要临时保留数字索引写法,可在项目根目录的l10n.yaml中添加配置禁用ICU消息格式校验:
synthetic-package: true arb-dir: lib/l10n output-localization-file: app_localizations.dart output-class: AppLocalizations use-icu-messages: false
注意:此方式会禁用复数、性别适配等ICU高级特性,仅建议短期过渡使用。
内容的提问来源于stack exchange,提问作者deczaloth
相关产品推荐
相关产品推荐

