Linesofcode
Linesofcode

Reputation: 5891

Flutter convert json data containing \n to new lines

My API sends a json with the following format:

{ status: false, message: 'This is an error \n\n with multi line!' }

I'd like to show the message and apply the new lines set in the json.

I'm using Dio package and I return the information like:

Response response = await Dio().post(...)
String message = response.data['message'];

This prints literally This is an error \n\n with multi line! instead of printing:

This is an error

with multi line!

enter image description here

If I type manually String message = 'This is an error \n\n with multi line!' it works great.

It seems to me that I have to use replaceAll function, but I don't know what to replace.

Upvotes: 0

Views: 1109

Answers (1)

Kohls
Kohls

Reputation: 908

Try using this

replaceAll('\\n', '\n');

Upvotes: 3

Related Questions