FormatException: Invalid UTF-8 byte (at offset 6)

Why do I get an error with this single line of code?

    final pdfString = " (T) 20.5 (–H) 4 (E) ]     % 5-element array of strings, reals, and intege";

why I get

FormatException: Invalid UTF-8 byte (at offset 11)

I copied this string from O’Reilly book.

And independently of the context, how to recover the pdfString if I have its byte representation only ?

>Solution :

As per the documentation for String.codeUnits:

An unmodifiable list of the UTF-16 code units of this string.

(Emphasis added.)

In effect, you are calling utf8.decode() on a UTF-16 string, which is not going to work.

If you provide more context about what you’re actually trying to do, I may be able to offer a suggestion.

Leave a Reply