New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserialize list of different types with same base type from JSON? #441
Comments
I'm not quite sure I follow. Which of You can use https://github.com/google/built_value.dart/blob/master/built_value/lib/src/uri_serializer.dart ...but if none of the classes are |
@dave26199 Some more explanation: This is pretty much the result of querying the firebase elements collection: [
{"order": 0, "name": "section header 0"}
{"order": 1, "title": "item name 1" , "color": "red"}
{"order": 2, "title": "item name 2" , "color": "green"}
{"order": 3, "name": "section header 1"}
{"order": 4, "title": "item name 3" , "color": "blue"}
{"order": 5, "title": "item name 4" , "color": "yellow"}
] It is basically a list of Now I want to deserialize this list received from Firestore into a Ideally there is a Later when iterating through that I have no idea how to do this kind of deserialization of multiple types into a Currently I use this code to deserialize BuiltList<T> documentsToBuiltList<T>(
List<DocumentSnapshot> documents, Serializer<T> serializer) {
return new BuiltList<T>(documents.map<T>((document) {
T builtDoc = standardSerializers.deserializeWith<T>(serializer, document.data);
return builtDoc;
}).toList());
} But I cannot figure out how this would look like for lists with different types in it? |
One way would be to figure out the type based on the values that are present. You'd need to do this for every item in the list, and call
If you can tell which type is needed based on the fields, this should be a reasonable approach. If you can't, that's a problem :) |
@davidmorgan yes, I can figure out what type each element is. The only thing I am still confused about is the actual implementation of the Would you be so kind and give me a very basic example for those so that I can have a Somehow I am confused about the |
Are |
@davidmorgan All these classes would be my own classes. Firestore just received |
Ah, okay. Then, I think you'll want something like this:
|
Hi
I have a pretty common use case but can't figure out how to do it with
BuiltList
.I have a list of
item
andsection
elements in my firestore collection:item type
section type
so in firestore my collection has many
item
s and manysection
s.Now if flutter I get this collection like this:
Now I want a
BuiltList
from that.So ideally I would have a
MenuElement
abstract
class
and
MenuItem
class
andMenuSection
class
that both implementMenuElement
and then a
BuiltList<MenuElement>
But I just can't figure out how to achieve that because
BuiltList
should beBuiltValue
and I can't tell them to implementMenuElement
.snapshot
which isJSON
with elements of the 2 different types.Is this at all possible?
It is a pretty common thing for lists to have sections and items and I hope there is a way to achieve this.
The text was updated successfully, but these errors were encountered: