chichi
chichi

Reputation: 3302

Flutter: add string to List with .addAll()

I have to add value that I receive from the api server.

print(userInfo['ID']['List'][0].runtimeType); // returns String
(userInfo['ID']['List'] as List).addAll(result['id_num']);

However, I am receiving I/flutter (23015): type 'String' is not a subtype of type 'Iterable<dynamic>'

How can I add value to the List in this case? Result['id_num'] also returns the type of String.

Upvotes: 0

Views: 1475

Answers (1)

CopsOnRoad
CopsOnRoad

Reputation: 267804

Using addAll:

addAll accepts an Iterable but you're giving it a String. All you need to do is create an Iterable from the String like this:

(userInfo['ID']['List'] as List).addAll([result['id_num']]);

Using add:

If you don't wish to use addAll, you can directly add String using add method:

(userInfo['ID']['List'] as List).add(result['id_num']);

Upvotes: 2

Related Questions