Optimizing UI rebuilds with 'ConsumerWidget' #571
-
I have a page whose small part (whether a class Abc extends StatelessWidget {
const Abc();
@override
Widget build(BuildContext context) {
return Consumer(
builder: (context, watch, child) {
final state = watch(someProvider);
return Scaffold(
body: child,
floatingActionButton: <depends on `state>,
);
},
child: SafeArea(...),
);
}
} I'm doing this so that the bulk of the UI doesn't get rebuilt unnecessarily, right now basically only the It seems a bit strange to have a It turns out
I could address the second issue by either introducing a Is it 'normal' to use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
You're probably over thinking. But maybe you want the following? Scaffold(
body: SafeArea...
floatingButton: Consumer(...)
) |
Beta Was this translation helpful? Give feedback.
-
After an update to 1.0.0 I will need to restructure this code anyway as the |
Beta Was this translation helpful? Give feedback.
-
It does seem like an odd limitation to not allow Consumer.builder to return null. Especially as Flutter regularly accepts Widget?, like in the case of scaffold.fab. This is certainly a valid use case and would prevent the entire scaffolds from rebuilding, which could be a significant cost savings. With that said, returning a container or sizedbox doesn't seem like a big problem to me... Its a common approach when you want to render nothing. |
Beta Was this translation helpful? Give feedback.
You're probably over thinking. But maybe you want the following?