Reputation: 51
I want to be able to remove a widget from tree, or remove a child of a widget. I tried to use states to change a widget to null but it's returning an error. Is there a solution ? Here is the error https://gist.github.com/litekangel/e2037cf5dc4dbd9c0c0a9860ad3b0270
I finally found a solution to hide/remove a widget : I just replaced it by an empty widget (may be I will add a small animation) but I am still looking for a cleaner way to do this.
Upvotes: 5
Views: 16486
Reputation: 8692
Another good approach to replace widgets in the tree is to give your widgets a key
.
(Flutter will rebuild the subtree if the key changes.)
So let's say you have a StreamBuilder, and for, whatever reason, need the child widget to be built from scratch every time, you can do this:
StreamBuilder(
stream: bloc.myDependencyChanged, // A hypothetical bloc which notifies when a relevant dependency has changed.
builder: (_, snap) {
final dep = MyChangingDependency();
// MyWidget will NOT get reused by flutter if the key changes.
return MyWidget(dep, key: ValueKey(dep.getId()));
},
);
Upvotes: 6
Reputation: 116728
If you want an empty widget, Container()
is a good choice.
Upvotes: 6
Reputation: 76213
In flutter you don't really update a tree of widget you actually generate a new tree every time you need to change it. So you only have to change the returned value of your widget build
function.
Upvotes: 8