Search code examples
dartstring-interpolation

Prevent line break in interpolated string


I having long string with interpolation in code. I am writing it as:

"""
$a: aaaaa
$b: bbbbb
$c: ccccc
"""

I do not want to write it in one line because readability. But in Text widget it displays also in column: enter image description here

How to prevent line breaking?


Solution

  • If you use a multi-line string literal, then you will get newlines in the string. You can remove them by doing:

    var string = """
    $a: aaaaa
    $b: bbbbb
    $c: ccccc
    """.removeAll("\n");
    

    That should leave you without any newlines.

    Alternatively you can use multiple adjacent string literals instead:

    var string =
      "$a: aaaa"
      "$b: bbbb"
      "$c: cccc";
    

    Adjacent string literals are combined into a single string value, and there are no extra newlines (if you want them, you have to add \n yourself). You also don't have to worry about leading whitespace on the lines.