Search code examples
flutterdartmessageformat

How to fix "ICU Syntax Error: Expected > "identifier" but found "0"." error in Flutter 3.7.0


After Updating to Flutter 3.7.0 i get this error message when i build my App:

[app_en.arb:scanCode_fieldNotMatched] ICU Syntax Error: Expected "identifier" but found "0". field to match is "{0}"

It seems as if something had changed in how the variables are written in the .arb localization files.


Solution

  • UPDATE 1: Escape syntax characters!

    If what you are trying is to use the characters {, }, ' (or any other syntax character for that matter) in your strings, then you will have to escape them. To do that enable the use-escaping flag by adding the following to l10n.yaml

    use-escaping: true
    
    

    Now use pairs of single quotes to escape syntax characters, like "{". To escape single quotes you just have to write it as a double-single quote as follows:

    {
      "some_text": "Using the '{' character '{isn''t}' trivial?"
    }
    

    More details on this in the flutter docu.


    Update 2: If you are using a Chinese Mirror for Flutter

    Follow details in this issue.


    Original answer to my punctual problem

    I found out that the reason for this error is that in Flutter 3.7

    Internationalization support has been completely revamped! [they]’ve completely rewritten the gen-l10n tool...

    as stated in a release post.


    Previously i was declaring strings in my .arb file as follows

    "scanCode_fieldNotMatched": "field to match is \"{0}\"",
    

    where afterwards i was replacing {0} by some other value.

    Well, it seems that now the gen-l10n tool takes what is between brackets as special parameters, and the name "0" is not accepted so i had to change my string to

    "scanCode_fieldNotMatched": "field to match is \"{value0}\"",
    

    and AppLocalizations can be now called as:

    AppLocalizations.of(context)!.scanCode_fieldNotMatched("something here to replace value0!")
    

    Further details on this can be found here: Placeholders, plurals, and selects in Flutter.