{

Dart| Flutter: Convert List of Strings into List<int> examples


How to Convert List of Strings into List  of int Dart and Flutter

This tutorial shows multiple ways to Convert List<String> into List<int> in Dart and flutter.

String and int types are different primitives 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.

How to Convert List of String into List of Int type in Dart

This example converts a list of string into a list of int in dart and flutter.

  • List has a string of numbers.
  • Iterate each element list using the map function.
  • map function has a function that applies to each element
  • Each element in a string is converted into an int using int.parse
  • Finally, returns the elements into a list using 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]

How to parse List of Int into List of String type in Dart

This example converts a list of ints into a list of String in dart and flutter.

The list of numbers is iterated using map() Each element in the map is applied with toString() to convert to a String. Finally, Return the 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]

Conclusion

Learned how to parse and convert a list of strings into a list of numbers and vice versa.

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.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart