{

Dart| Flutter: Convert List of Dynamic into List of String examples


In Dart and flutter, this tutorial explains how to convert List<Dynamic> to List<String> and vice versa. How to Convert List of Dynamic into List of String  in Dart and Flutter

A dynamic type is a primitive type that may hold any dynamic value, such as an integer, a string, or a double.

Because a string is a primitive type that stores a collection of characters, automated conversion to/from dynamic is not possible.

You can check on How to convert dynamic to string vice versa or vice versa in Dart and flutter.

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

In dart and flutter, this example converts a list of strings to a list of dynamic.

  • List has an array of strings.

  • List. from() has a named constructor that takes a list of strings. It is a shallow copy of a list.

void main() {
  List<String> numbers = <String>['one', 'two', "four"];
  print(numbers.runtimeType);
  List<dynamic> dynamicValues = List<dynamic>.from(numbers);

  print(dynamicValues.runtimeType);
  print(dynamicValues);
}

Output:

JSArray<String>
JSArray<dynamic>
[one, two, four]

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

In dart and flutter, this example converts a list of dynamic types to a list of Strings.

map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list.

void main() {
  List<dynamic> numbers = <dynamic>['one', 'two', "four"];
  print(numbers.runtimeType);
  final List<String> strs = numbers.map((e) => e.toString()).toList();

  print(strs.runtimeType);
  print(strs);
}

Output:

JSArray<dynamic>
JSArray<String>
[one, two, four]

Conclusion

Learned how to parse and convert a list of strings into a list of dynamic 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

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

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

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example