Matthew Ramsey
Matthew Ramsey

Reputation: 41

Convert Firebase Nested Map to List<Object>

I have the following data in Firebase:

enter image description here

In Flutter, I have the following two objects:

enter image description here

enter image description here

I'm trying to convert the nested map of sets in Firebase to the List in Flutter but am having issues. What is the best way to convert a Map object with Map fields into a List in Flutter?

I've tried mapping the sets via this method and two different techniques: enter image description here

enter image description here

I know I'm having an issue handling the map object and I've received different errors depending on my approach but usually it is similar to one of the following:

Error 1: "type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List'"

Error 2: "type '(dynamic) => ExerciseSet' is not a subtype of type '(dynamic, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'"

Thank you for any help!

Upvotes: 1

Views: 1370

Answers (2)

Matthew Ramsey
Matthew Ramsey

Reputation: 41

I can't believe after spending so many hours and finally deciding to post... I fixed it right after. Here's my working code:

enter image description here

Thank you for the quick response @Sanjay! I'll give yours a try to see if that works as well.

Upvotes: 0

Sanjay Sharma
Sanjay Sharma

Reputation: 4035

If you have List of objects inside one object then you need to parse the list separately as the following

sets = (json['sets'] as List)
           .map((data) => ExerciseSet.fromJson(data))
           .toList();

Here json is decoded JSON.

Upvotes: 1

Related Questions