initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:
- In initState, subscribe to the object.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState().
Implementation
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (!mounted || _submitted) return;
_submitted = true;
final pageState = context.findAncestorStateOfType<BasePageState>();
final flow = pageState?.widget.flowInstance;
final flowState = flow?.flowState ?? {};
try {
// Get the required data from flowState and set to dataFields (some list of the same type)
// To get the required value from the flowState, pass the list of keys in the options
// Use that list to populate the dataFields list from flowState and pass to creator
final pageFields = <String, dynamic>{};
pageFields.addAll(flowState);
final pageDataHandler = BasePage.getPageDataHandler();
final msgCreator = pageDataHandler?.msgCreator as PageMsgCreator;
final msg = msgCreator.createMessageFromStep(
widget.stepEnum,
pageFields,
);
msg.buildContext = context;
if (msg == null) {
throw hujFrameworkException(
message: 'No message generated for save step.',
hujType: hujErrorType.framework,
);
}
await pageDataHandler?.handleMessage(msg);
submitResponse({});
} catch (e, stackTrace) {
__.error("Error saving data: $e, stackTrace: $stackTrace");
completeStep(result: StepResult.stepError);
}
});
}