flutter-form-builder-ecosystem / flutter-form-builder-ecosystem/flutter_form_builder

[FlutterFormBuilder]: using context.findAncestorStateOfType in FormBuilder.of

Open
#1,474 9 comments 1 reaction 1 assignee Claimed by @deandreamatias View on GitHub
bug
Dominant language
Dart
Stars
1.6k
Forks
561
PR merge metrics
No merged PRs in 30d

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Package/Plugin version

9.7.0

### Platforms

- [ ] Android
- [ ] iOS
- [ ] Linux
- [ ] MacOS
- [ ] Web
- [ ] Windows

### Flutter doctor

Flutter doctor

```bash

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

```dart
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.
```dart
static FormBuilderState? of(BuildContext context, [bool listen = false]) {
if (listen) {
return context
.dependOnInheritedWidgetOfExactType<_FormBuilderScope>()
._formState;
}
return context.findAncestorStateOfType();
}
```

### Steps To Reproduce

1. Create any widget.
2. Wrap it in FormBuilder
3. In the widget's build method, call FormBuilder.of(context)

### Aditional information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.