An RMKey acts similarly to Flutter's GlobalKey, allowing you to control and synchronize StateBuilder observer widgets from outside their own builder closures.
By assigning an RMKey to the rmKey parameter of a StateBuilder, you can update the state from any other widget in the tree (e.g., a button in a different part of the Scaffold) by modifying the RMKey.value property. Other widgets can also subscribe to the same RMKey via the observe parameter to stay synchronized.
// 1. Define the key
final switchKey = RMKey(true);
// 2. Assign to a StateBuilder to control/notify it
StateBuilder<bool>(
observe: () => RM.create(true),
rmKey: switchKey,
builder: (ctx, switchRM) {
return Switch(
value: switchRM.value,
onChanged: (value) => switchRM.value = value,
);
},
)
// 3. Subscribe another widget to the same key
StateBuilder<bool>(
observe: () => switchKey,
builder: (ctx, switchRM) {
return Switch(
value: switchRM.value,
onChanged: (value) => switchRM.value = value,
);
},
)
// 4. Update from anywhere
switchKey.value = !switchKey.value;