How to pick up text from a multi-line textfield in a list with one item per line

Asked

Viewed 687 times

1

I wonder if you have any way to take a text from a textfield and turn it into a list.

Text with an item on each line.

To

B

C

D

List = [learns A', learns B', learns C', learns']

I tried to use one for but I don’t know the best way to identify that the line is dead and they’re gonna start the other.

  • Your question got a little confused... Do you want to, for example, type something in Textfield and when you click a button, insert the record in a list? If you give us an example of what you have already done we can help you, because if it is as I said, it is possible yes and very simple.

2 answers

1


  • Vacilei de não ter pesquisado o split em Dart/flutter, muito obrigado!

  • Fernando, I am happy to have helped! I emphasize here the importance of documentation, for any technology you may use. Success!

  • Thanks so much for the answer and the abs tip.

1

Let’s say this is your Textfield

TextField(
  keyboardType: TextInputType.multiline,
  maxLines: 10,
);

You can get its value by adding a type controller TextEditingController, or simply by adding a callback methodically onChanged, to facilitate things we will add to callback

TextField(
  keyboardType: TextInputType.multiline,
  maxLines: 10,
  onChanged: (novoTexto) {}
);

As already suggested by João Monteiro’s reply, you need to make a split in the variable that contains your text, which in your case would be per line, for this you can use the character \n, are would be:

onChanged: (novoTexto) {
  var lista = novoTexto.split('\n');
}

So every time your input value changes, the method onChanged will be called with the function you passed, within this you are "divined" the text in a list of texts, each entry of this list corresponds to a line of text;

  • Guy was exactly that, thank you very much! I had already used the split but in python, waved rs. Anyway it was worth even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.