THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial shows multiple ways to Convert List<String>
into List<int>
in Dart and flutter.
String and int types are different primitive and store different values, So automatic conversion are not possible. You can check on How to convert String to int or vice versa in Dart and flutter.
This example converts a list of string
into a list of int
in dart and flutter.
map
function.map
function has a function that applies to each elementint.parse
toList()
void main() {
List<String> strs = <String>["11", "12", "5"];
print(strs.runtimeType);
List<int> numbers = strs.map(int.parse).toList();
print(numbers.runtimeType);
print(numbers);
}
Output:
JSArray<String>
JSArray<int>
[11, 12, 5]
This example converts a list of int into a list of String in dart and flutter.
List of numbers are iterated using map()
Each element in the map is applied with toString()
to convert to String.
Finally, Return list using toList()
method
void main() {
List<int> numbers = <int>[11, 12, 5];
print(numbers.runtimeType);
final List<String> strs = numbers.map((e) => e.toString()).toList();
print(strs.runtimeType);
print(strs);
}
Output:
JSArray<int>
JSArray<String>
[11, 12, 5]
Learned how to parse and convert a list of strings into a list of numbers and vice versa.
🧮 Tags
Recent posts
Multiple ways to iterate a loop with index and element in array in swift How to reload a page/component in Angular? How to populate enum object data in a dropdown in angular| Angular material dropdown example How to get the current date and time in local and UTC in Rust example Angular 13 UpperCase pipe tutorial | How to Convert a String to Uppercase exampleRelated posts