'cannot able to create List of string in dart flutter
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);
Solution 1:[1]
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.
Solution 2:[2]
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'
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Manish |
| Solution 2 | Tuan |
