如何修复“ICU 语法错误:预期>“标识符”,但发现“0”。” Flutter 3.7.0 中的错误

Dec*_*oth 5 messageformat dart flutter

更新到 Flutter 3.7.0 后,我在构建应用程序时收到此错误消息:

[app_en.arb:scanCode_fieldNotMatched] ICU 语法错误:预期为“标识符”,但发现为“0”。要匹配的字段是“{0}”

.arb 本地化文件中变量的写入方式似乎发生了变化。

Dec*_*oth 3

更新 1:转义语法字符!

\n

如果您尝试在字符串中使用字符{, }, \'(或任何其他语法字符),那么您将必须转义它们。为此,请use-escaping通过将以下内容添加到l10n.yaml来启用该标志

\n
use-escaping: true\n\n
Run Code Online (Sandbox Code Playgroud)\n

现在使用成对的单引号来转义语法字符,例如“{”。要转义单引号,您只需将其写为双单引号,如下所示:

\n
{\n  "some_text": "Using the \'{\' character \'{isn\'\'t}\' trivial?"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

有关此内容的更多详细信息,请参阅flutter docu

\n
\n

更新2:如果你使用的是中文镜像Flutter

\n

请关注本期的详细信息。

\n
\n

我的准时问题的原始答案

\n

我发现这个错误的原因是在Flutter 3.7中

\n
\n

国际化支持已彻底改进![他们]\xe2\x80\x99已经完全重写了gen-l10n工具...

\n
\n

正如发布帖子中所述。

\n
\n

以前我在.arb文件中声明字符串如下

\n
"scanCode_fieldNotMatched": "field to match is \\"{0}\\"",\n
Run Code Online (Sandbox Code Playgroud)\n

之后我{0}用其他值替换。

\n

好吧,看来现在该gen-l10n工具将括号之间的内容作为特殊参数,并且该名称"0"不被接受,所以我不得不将字符串更改为

\n
"scanCode_fieldNotMatched": "field to match is \\"{value0}\\"",\n
Run Code Online (Sandbox Code Playgroud)\n

现在可以AppLocalizations称为:

\n
AppLocalizations.of(context)!.scanCode_fieldNotMatched("something here to replace value0!")\n
Run Code Online (Sandbox Code Playgroud)\n

有关此内容的更多详细信息,请参阅:Placeholders,plurals, and selects in Flutter

\n