bilbo_bo
bilbo_bo

Reputation: 89

Access index of a list in Flutter/Dart

I would like to access an index of a string (e.g. 'Hello') and print it to the app (e.g. letter 'o'). This string has to be the input of the user. This can be easily done using Python but I have to use Flutter/Dart in order to do it (mobile development).

Here is just an example of how this would be solve using Python:

my_word = input("Insert your word here: ")

print(my_word[4])

Upvotes: 0

Views: 1365

Answers (3)

Stewie Griffin
Stewie Griffin

Reputation: 5638

  1. Use TextField to retrieve user's input:

A text field lets the user enter text, either with hardware keyboard or with an onscreen keyboard.

  1. Use Text to display the string that you retrieve:

The Text widget displays a string of text with single style.

  1. Use the [] (index) operator to get the character at the index you specify:
final str = "Hello World";

print(str[4]); // Prints o

Upvotes: 1

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63864

dartPad

main() {
  String val = "hello";
  print(val[4]);
}

Upvotes: 0

esentis
esentis

Reputation: 4726

You can try split the string's letters in an array first.

my_word.split('')[4]

Upvotes: 0

Related Questions