raw text

Quick Tip – How to use special characters in Flutter

Special characters in Flutter

I think every programing language uses special characters. They are used everywhere. So, what are the special characters? A simple explanation will be, special characters are all symbols that cannot be rendered as text.

In Flutter, you can use special characters in your text by specifying the Unicode character code for that character. For example, to show the copyright symbol (©), you can use the following code:

Text('Copyright \u00A9 2021')

How to use special characters in Flutter

In Dart (the programming language used to build Flutter apps), you can use the backslash () character to escape special characters in a string. When you escape a character, you are telling the compiler to treat the character as a normal “letter” rather than as a special character with a special meaning.

For example, if you want to include a quotation mark (“) in a string, you can escape it by preceding it with a backslash:

String s = "This is a string with a \"quotation mark\" in it.";

This will allow you to include the quotation mark in the string without causing any errors.

There are several other special characters that you can escape in this way, including the backslash itself (\), the newline character (\n), and the tab character (\t).

By escaping special characters in this way, you can include them in your strings without causing any issues with the compiler.

Here is another example.

        Text(
          'Is\'t and are\'t are the examples',
        ),

Also as a special character, you can use Unicode.

As in the above example, Unicode is used quite often. So how to use Unicode inside your String. Simply place Unicode in a place where you want your special character. To be more specific In Flutter we Unicode escape sequence. And, what’s the difference? The Unicode escape sequence uses ( \ ) as a first character, for example, Unicode ( U+0026 ) as an escape character is ( \u0026 ). So, how to convert Unicode to an escape character? Start with ( \ ) then use a small letter as the first letter ( u ), remove ( + ) and then use the rest of the characters. I hope it makes sense. I think you can see the difference.

“U+0026” “\u0026”

In this example, the \u00a9 code is used to display the copyright symbol (©) in the text.

        Text(
          '2022 \u00a9 Author\'s Name',
        ),

Online you can find a lot of websites where you can find the special character you need. One page worth visiting is Wikipedia with a Unicode list: https://en.wikipedia.org/wiki/List_of_Unicode_characters.

How to use a raw string in the Text widget in Flutter

To render all the special characters inside a string you can create a raw string by prefixing it with ( r ). This way the ( \ ) (backslash) will be treated as a normal letter character. The example below shows the next line character ( \n ) ignored and instead it is rendered as normal text.

        Text(
          r"flutterassets.com \n raw string example.",
        ),

Here is another example.

String rawString = r'This is a raw string. It will not interpret
escape sequences, such as
newlines or tabs.';

This will display the string as-is, without interpreting any escape sequences. For example, the \n character will not be treated as a newline, but will be displayed as part of the string.

As you can see the string isn’t divided into two lines.

raw text

Note that you can also use triple quotes (''' or """) to define a raw string that spans multiple lines.

Text(r'''This is a
raw string that
spans multiple
lines.''')

The raw string in this example is defined using triple quotes ('''), which allows it to span multiple lines. The r character at the beginning of the string signifies that it is a raw string, and any special characters within it will be interpreted literally, rather than being treated as escape sequences.

You can read more about the Text widget in this post:

Flutter Basics – What is and how to use Text widget in Flutter