Home > Back-end >  cannot able to create List of string in dart flutter
cannot able to create List of string in dart flutter

Time:04-20

I am try to create a List of string

i need a list like ["9999999998","9876788667","8753578546"].

but what i get us [9999999998,9876788667,8753578546]

this is my list declaration

List<String> numbers = <String>[];

adding string to the list

numbers.add(numberController.text);

CodePudding user response:

First, avoid to using List<String> numbers = <String>[];. Use List<String> numbers = []; or var numbers = <String>[]; is enough.

Second, your code is looking fine, it type is String already, but dart console not print double quote "" of a string. You can check the type by using runtimeTupe.

Example with dynamic list:

void main() {
  var numbers = <dynamic>[
    "123",
    123,
    123.2,
  ];
  
  numbers.forEach((e) {
    print("type of '$e' is '${e.runtimeType}'");
  });

  // --- Resutl
  type of '123' is 'String'
  type of '123' is 'int'
  type of '123.2' is 'double'
}

Example with your list:

void main() {
  var numbers = <String>["9999999998","9876788667","8753578546"];
  
  numbers.forEach((e) {
    print("type of '$e' is '${e.runtimeType}'");
  });

  // --- Resutl
  type of '9999999998' is 'String'
  type of '9876788667' is 'String'
  type of '8753578546' is 'String'
}

CodePudding user response:

Can you verify it by running print(numbers[0].runtimeType);

TextEditingController.text always gives you string even if you're typing numbers in your TextField Widget. Hence the list you have is actually a list of Strings.

  • Related