-
Notifications
You must be signed in to change notification settings - Fork 183
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
Question: How to reference a parent node from a child node. #1212
Comments
Thanks Alex. I don't have suggestions for a good way to achieve this in Flutter; but what you're describing sounds along the right lines. When I've done this in my own code--but not Flutter code--I've done it by adding IDs to the data classes, then having a global map from ID to instance. I've used a type Maybe there is something already in Flutter that gives you an ID to instance mapping you could use. Or, perhaps you could use the |
Thanks for clarifying! Do you have an example of the |
Nothing I can point to unfortunately. The idea is similar to your example, except:
You might reasonably ask, why go to all the trouble of having immutable types only to introduce something like pointers back into the equation--since what the ID points to can change over time. The answer is, you can control updates to that global map in one place, e.g. accumulating then committing changes to it, rolling back if needed, etc. So you still have plenty of control. |
Thanks again! I will try that. It sounds promising, because like that I can create a ViewModel fairly easy by wrapping a data class inside a This resolves a lot of difficulties. All but one: |
Yes, you will need something to find and remove dead nodes. If all your nodes implement an interface that returns all the |
If I have a list of child nodes, I can simply call this list from the parent object.
The child node, on the other hand, has no reference to the parent node. In a deeply nested data model, however, a reference from child nodes to the parent node would be extremely convenient. For example, a Flutter widget could have only one child node passed to it for display. However, it might be necessary to retrieve or change information about the fathers. Without the reference to the father, the entire chain from the leaf node to the root node would have to be passed to the widget for this.
Value types, of course, cannot store references, only values.
The only idea I can think of is to leave the model as it is:
The reference of the parent node in the child node, on the other hand, could be stored in a ViewModel. Here as a global map as a static field of the extension class
GetParent
and a getterparent
as an extension method for easier calling.Then the getter
parent
could simply be called on the child node.However, the
childToParent
map must also be filled manually for this. It would be nicer if the child node could be automatically linked to the father node while the father is being created.Is there a better way to implement this in
built_value
?Are there best practices on how to add such references in a ViewModel without changing the model?
The text was updated successfully, but these errors were encountered: