Imagine I have the following situation:
We have a stateful widget A, which stores some map.
Widget A passes this map to stateful widget B, which stores it as a local variable in initState
.
Then, any changes in this local map (within widget B) somehow affect the original map (the one in widget A).
How is it possible?
Some basic code for illustation:
class A extends StatefulWidget {
@override
_AState createState() => _AState();
}
class _AState extends State<A> {
Map _someMap = {
'key': 'Some value',
'anotherKey': 'Some another value',
};
@override
Widget build(BuildContext context) {
return B(
inheritedMap: _someMap,
);
}
}
class B extends StatefulWidget {
final Map inheritedMap;
B({this.inheritedMap});
@override
_BState createState() => _BState();
}
class _BState extends State<B> {
Map _inheritedMapCopy;
@override
void initState() {
_inheritedMapCopy = widget.inheritedMap;
super.initState();
}
@override
Widget build(BuildContext context) {
return TextFormField(
onChanged: (val) {
print('Parent data before change:');
print(widget.inheritedMap);
_inheritedMapCopy['key'] = val;
print('Parent data after change:');
print(widget.inheritedMap); /// Contains all the changes for _inheritedMapCopy
},
);
}
}