You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The method 'detach' isn't defined for the class 'FocusScopeNode' #31784
After a flutter upgrade and a packages upgrade I'm having this error:
The method 'detach' isn't defined for the class 'FocusScopeNode' on:
FocusScope.of(context).detach();
[✓] Flutter (Channel master, v1.5.9-pre.46, on Mac OS X 10.14.4 18E226, locale it-IT)
• Flutter version 1.5.9-pre.46 at /Users/miottoan/flutter
• Framework revision e62b06a (27 minutes ago), 2019-04-29 05:58:18 -0400
• Engine revision 48a5ce8
• Dart version 2.3.0 (build 2.3.0-dev.0.3 c46deebfb6)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/miottoan/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
• All Android licenses accepted.
[✓] iOS toolchain - develop for iOS devices (Xcode 10.2.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.2.1, Build version 10E1001
• ios-deploy 1.9.4
• CocoaPods version 1.6.0
[✓] Android Studio (version 3.4)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 35.0.1
• Dart plugin version 183.5901
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
[✓] VS Code (version 1.33.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 2.26.0
Yes, this is due to a breaking change in FocusScopeNode. Are you hosting the focus node (calling reparentIfNeeded), or just creating one to pass in to a TextField or FocusScope widget?
Actually, if you're calling detach, you're probably hosting the node. There have been several changes in the way focus nodes work for widgets that are hosting nodes:
In order to host a node, you need to attach to the node in your initState method to create a FocusAttachment, passing in the context:
Where you called detach before on the focus scope node, you will now call detach on the focus attachment.
_nodeAttachment.detach();
However, I'd like to also point out that with the new focus API, it's less common to need to host a node yourself: you can just use the Focus and FocusScope widgets to handle all of the hosting mechanics (the things above), and simply pass the scope node to a FocusScope widget instead. You will still need to call dispose on a node that you own yourself. Even owning your own focus node isn't as necessary, unless you need an ancestor widget to be able to tell descendant nodes to be focused, since the Focus and FocusScope widgets will create and dispose of their own nodes if you don't supply one.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
HansMuller commentedon Apr 29, 2019
CC @gspencergoog
gspencergoog commentedon Apr 29, 2019
Yes, this is due to a breaking change in
FocusScopeNode. Are you hosting the focus node (callingreparentIfNeeded), or just creating one to pass in to aTextFieldorFocusScopewidget?gspencergoog commentedon Apr 29, 2019
Actually, if you're calling
detach, you're probably hosting the node. There have been several changes in the way focus nodes work for widgets that are hosting nodes:attachto the node in yourinitStatemethod to create aFocusAttachment, passing in the context:dispose, you need to calldisposeon the focus scope node:reparentinstead ofreparentIfNeeded:detachbefore on the focus scope node, you will now call detach on the focus attachment._nodeAttachment.detach();However, I'd like to also point out that with the new focus API, it's less common to need to host a node yourself: you can just use the
FocusandFocusScopewidgets to handle all of the hosting mechanics (the things above), and simply pass the scope node to aFocusScopewidget instead. You will still need to calldisposeon a node that you own yourself. Even owning your own focus node isn't as necessary, unless you need an ancestor widget to be able to tell descendant nodes to be focused, since theFocusandFocusScopewidgets will create and dispose of their own nodes if you don't supply one.AndreaMiotto commentedon Apr 30, 2019
It is still unclear to me how to close the keyboard on the textfield, at the moment I'm focusing a new FocusNode() like this:
FocusScope.of(context).requestFocus(new FocusNode());But I know that this is not the proper way.
gspencergoog commentedon Apr 30, 2019
You can use this:
FocusScope.of(context).unfocus()github-actions commentedon Aug 30, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.