Repository navigation
[FlutterFormBuilder]: using context.findAncestorStateOfType in FormBuilder.of #1474
Copy link
Copy link
Open
Copy link
Labels
bugSomething isn't workingSomething isn't working
Description
Is there an existing issue for this?
- I have searched the existing issues
Package/Plugin version
9.7.0
Platforms
- Android
- iOS
- Linux
- MacOS
- Web
- Windows
Flutter doctor
Flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, 3.27.3, on macOS 15.2 24C101 darwin-arm64, locale ru-UZ)
✗ Downloaded executables cannot execute on host.
See https://github.com/flutter/flutter/issues/6207 for more information.
Flutter requires the Rosetta translation environment on ARM Macs. Try running:
sudo softwareupdate --install-rosetta --agree-to-license
[!] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
✗ cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/to/macos-android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2024.3.2.2)
[✓] Connected device (5 available)
! Error: Browsing on the local area network for iPhone 13 Magomed . Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)
[!] Network resources
✗ A cryptographic error occurred while checking "https://pub.dev/": Connection terminated during handshake
You may be experiencing a man-in-the-middle attack, your network may be compromised, or you may have malware installed on your computer.
✗ An HTTP error occurred while checking "https://storage.googleapis.com/": Connection closed before full header was received
✗ A cryptographic error occurred while checking "https://maven.google.com/": Connection terminated during handshake
You may be experiencing a man-in-the-middle attack, your network may be compromised, or you may have malware installed on your computer.
✗ A network error occurred while checking "https://cocoapods.org/": Connection reset by peer
✗ A cryptographic error occurred while checking "https://github.com/": Connection terminated during handshake
You may be experiencing a man-in-the-middle attack, your network may be compromised, or you may have malware installed on your computer.
! Doctor found issues in 3 categories.
Minimal code example
Code sample
Builder(
builder: (context) {
final formState = FormBuilder.of(context);
},
);Current Behavior
Currently, if you call FormBuilder.of in builder, it calls context.findAncestorStateOfType() which reduces the performance of the application because findAncestorStateOfType enumerates all parent nodes in the tree until it finds FormBuilderState.
In addition, with this approach, the current element is not updated when FormBuilderState changes.
Expected Behavior
FormBuilder.of must use context.dependOnInheritedWidgetOfExactType.
static FormBuilderState? of(BuildContext context, [bool listen = false]) {
if (listen) {
return context
.dependOnInheritedWidgetOfExactType<_FormBuilderScope>()
._formState;
}
return context.findAncestorStateOfType<FormBuilderState>();
}Steps To Reproduce
- Create any widget.
- Wrap it in FormBuilder
- In the widget's build method, call FormBuilder.of(context)
Aditional information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Type
Projects
- StatusShow more project fieldsReady
Activity
itsUndefined commentedon Apr 17, 2025
@deandreamatias any chance this can get prioritized? Should be a very easy fix.
deandreamatias commentedon Apr 17, 2025
@itsUndefined had a lot things to solve on https://github.com/flutter-form-builder-ecosystem and I don't have time enogth to solve all :(
But I'm totally open to review a PR if someone propose a solution for this
deandreamatias commentedon Apr 21, 2025
Hi @itsUndefined !
I need a minimal code example to can execute:
mainDart methodAlso, I need:
Example:
Thanks a lot
deandreamatias commentedon Apr 21, 2025
Some references for implementation:
itsUndefined commentedon Apr 21, 2025
Thank you for the references.
Here is a minimal example showing the original issue description, regarding the widget not updating on state update.
Notice that no matter what,
_NestedWidgetnever rebuilds and "Form is invalid" is always displayed.I recommend adding a listen option on the Of method with listen=false as the default to keep existing behavior as is. In a future major release we can make this default to true as this appears to be the default pattern. This will be a breaking change.
deandreamatias commentedon Apr 22, 2025
I'm trying to figure this out, but it's a bit wild. I'm going to take a deeper look to see if I get any solutions.
deandreamatias commentedon Apr 22, 2025
Related with #1129
itsUndefined commentedon Apr 22, 2025
Thanks for investigating this. I might find some time to troubleshoot and come up with a solution.
deandreamatias commentedon Apr 22, 2025
I created a draft PR. Take a look on there branch.
I tried to solve the issue, but the _NestedWidget still without update form isValid. Maybe need force rebuild formState in some way