Skip to content
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

Handling of deep hierarchy #453

Closed
jonasbark opened this issue Jul 13, 2018 · 2 comments
Closed

Handling of deep hierarchy #453

jonasbark opened this issue Jul 13, 2018 · 2 comments

Comments

@jonasbark
Copy link

This is probably more of a question than an issue - I hope this is the right place for it.

This is an example model:

{
    "title": "Title",
    "items": [
        {
            "title": "Title 1",
            "items": [
                {
                    "title": "Titel 2",
                    "items": [                        
                    ]
                }
            ]
        }
    ]
}

So an instance of an Item has a BuiltList<Item> attribute what might ultimately create a deep hierarchy.

There's no simple way to update the Item with "Titel 2":

items.rebuild((b) => b.items[0].items[0].title = "new title") 
//does not work as we're getting builders when accessing an item via array index
//... and we don't know the correct indexes

Is this even the right approach?
Is it better to create a flat hierarchy with lots indexes pointing to each other?

Thank you!

@davidmorgan
Copy link
Collaborator

Yeah, this is not handled well.

It works if you know how many items you'll have. For example if you always had three items, you could have fields called first, second and third. Then you could do

items.rebuild((b) => b..first.first.title = 'new title');

--but with arbitrary length lists this doesn't make sense.

Maybe it would be handy to expand on the use case a bit. For any particular generic structure I guess you can write a generic accessor for it, something like

void setTitleAt(ItemsBuilder itemsBuilder, Iterable<int> indices, String newTitle) {
  // descend into itemsBuilder.items, applying indices, then apply 'newTitle'
}

...

setTitleAt([0, 0], 'Titel 2');

but it's not clear whether this would be helpful for your specific case.

The other possibility would be an extension to built_collection, but this seems hard: google/built_collection.dart#83

Thanks!

@jonasbark
Copy link
Author

Thanks for the input!

As things would get really complicated with deep hierarchies I decided to drop that idea and implement a structure similar to the tree-view example from redux.js:
https://codesandbox.io/s/github/reactjs/redux/tree/master/examples/tree-view

So far it works really well using BuiltMap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants