Home > Net >  Set the initialValue of a TextInput as a multi-line String
Set the initialValue of a TextInput as a multi-line String

Time:11-05

I have a multi-line textInput in my application that looks like this:

TextFormField(
  maxLines: 5,
  minLines: 3,
  initialValue: object.textValue
)

It works fine when the user inputs new values.
However, when I need to load an existing value the input is not applying the line breaks.

This is an example of the text value I need to load:

"line 1\nline 2\n\nline 4"

This is how it looks:

enter image description here

How can I display the text respecting the line breaks?

CodePudding user response:

you can assign it with a TextEditingController :

  final controller = TextEditingController();
  controller.text = "test \n test \n";

CodePudding user response:

I created this dartpad to test and it worked, as many pointed out via comments.

So, reviewing my code I found the issue is introduced by a "knob" I'm using (with the Widgetbook package):

context.knobs.text(...)

It is probably escaping the string before passing it to my widget.

  • Related