I have a List<String>
and I need to change it to List<dynamic>
in Dart 2.
First, I tried this code but it doesn't work:
var str_list = ["aa", "bb"];
// fails - is not dynamic
// gives List<String>
List<dynamic> target_list_0 = str_list;
Later, I tried these 2 ways:
// succeeds - is dynamic
// gives List<dynamic>
var target_list_1 = [] + str_list;
// succeeds - is dynamic
// gives List<dynamic>
var target_list_2 = List<dynamic>.from(str_list);
It works but I am not sure it is the best (or idiomatic) way to fix the problem. I would like to write my Dart-2 code as clean as possible, could you help?
Edit
@matanlurey asked clear example where this pattern is necessary and how I am using it.
I am developing something like twitter but more complex (not only #hashtags and @mentions but words that have internal structure). So, I make some modification of the source text, then split it by whitespace (or parentheses) to words and then examine word by word. If the word is special I change the string to a map (containing parsed info). The change is happening in-place in the list. And that is where I need it dynamic
. I have this source code:
var split_regexp = new RegExp(r"[^\s\(\)]+|[\s\(\)]+");
var body_matches = split_regexp.allMatches(body_part);
var body = [] + body_matches.map(
(m) => m.group(0)
).toList();
// ... processing it ...
Currently, I am rewriting it from javascript (to which I rewrote it from python). Both the languages are able to catch the text by which I split words, it is much easier:
// python
body = re.split(r"([\s\(\)]+)", body)
// javascript
body = body.split(/([\s\(\)]+)/g)
Is it possible to write it so simple in Dart? I think not - because I need the splitter whitespace (or splitter parentheses or anything in future versions of my app) and dart split
doesn't catch it.
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.
We have 3 steps to convert an Object/List to JSON string: create the class. create toJson() method which returns a JSON object that has key/value pairs corresponding to all fields of the class. get JSON string from JSON object/List using jsonEncode() function.
Create Dynamic ListView using ListView. builder() And you would not know the number of elements in the list beforehand. In such scenarios, you can use ListView. builder() constructor of ListView class to create a dynamic list of widgets in ListView widget.
List.from(...)
is going to be the most idiomatic way, today. In fact, the style guide now recommends it if you are trying to change the type of an existing list:
// If you want to change the type, then calling List.from() is useful:
var numbers = [1, 2.3, 4]; // List<num>.
numbers.removeAt(1); // Now it only contains integers.
var ints = new List<int>.from(numbers);
as clean as possible
Probably you don't want List<dynamic>
at all, in that case. It might help if you can give clear examples where this pattern is necessary and how you are using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With