fix error LateInitializationError: Local 'cancelFunc' has not been initialized #176
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The error LateInitializationError: Local 'cancelFunc' has not been initialized occurs because the cancelFunc variable is declared as late but is not initialized before it is used. This typically happens when the cancelFunc is referenced in a closure or callback before it is assigned a value.
I need to ensure that cancelFunc is initialized before it is used. One way to do this is to declare cancelFunc as a non-late variable and initialize it before it is used in the dismissFunc.
Explanation of Changes
The cancelFunc is no longer declared as late. Instead, it is initialized with a dummy function (CancelFunc cancelFunc = () {};) to ensure it has a value before it is used in the dismissFunc.
After the dismissFunc is defined, the cancelFunc is reassigned to the result of showWidget. This ensures that cancelFunc is properly initialized before it is used.
Why This Fix Works
The issue arises because the dismissFunc references cancelFunc before it is assigned a value. By initializing cancelFunc with a dummy function and then reassigning it later, we ensure that cancelFunc is never in an uninitialized state when it is used.