My code implementation in flutter InheritedWidget / InheritedModel is the recommended one. However i have a little question.

Let's say i have an inheritedWidget called MyInheritedWidget which holds some reactive data. What would be the best way of accesing that data ?

Widget build(BuildContext context) {
    MyData data= MyInheritedWidget.of(context).someData;
    return ExampleWidget(
   //Using the data here
   data : data.someData.data,
   moreData : data.moreData
   sameData:data.someData.data,
);

or

Widget build(BuildContext context) {
    return ExampleWidget(
   //Using the data here
   data : MyInheritedWidget.of(context).someData.data,
   moreData : MyInheritedWidget.of(context).someData.moreData
   sameData: MyInheritedWidget.of(context).someData.data,
);

i know that we call context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>(); and that subscribes the context to the data so when it changes it rebuilds. Aren't we subscribing multiple times by calling MyInheritedWidget.of(context)? Or the performance it's the same ? I think the flutter team is smart enough to subscribe only once and return the data if we are subscribed. But which approach is better for performance?

This is not about readability, it's about performance. Since i could easily use an extension in BuildContext to convert MyInheritedWidget.of(context).someData to context.someData.


Solution 1: Rémi Rousselet

Inherited.of(context) is relatively equivalent to:

Map<Type, Inherited> ancestors;

return ancestors[context.widget.runtimeType];

As such, reading an InheritedWidget multiple times in the same widget isn't really problematic.

But extracting its value in a variable will most likely be slightly faster, as reading a Map is slower than just reading a variable.