Skip to content

The method 'detach' isn't defined for the class 'FocusScopeNode' #31784

Closed

Description

@AndreaMiotto

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

[✓] Connected device (1 available)
• iPhone Xʀ • 28F1BE2E-AE5F-4CAF-A1D8-0FBD47BA45FA • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)

Activity

  1. HansMuller commented on Apr 29, 2019

    @HansMuller
    Contributor
  2. gspencergoog commented on Apr 29, 2019

    @gspencergoog
    Contributor

    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?

  3. gspencergoog commented on Apr 29, 2019

    @gspencergoog
    Contributor

    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:

    1. 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:
      FocusScopeNode _node;
      FocusAttachment _nodeAttachment;
      @override
      void initState() {
        super.initState();
        _node = FocusNode();
        _nodeAttachment = _node.attach(context);
      }
    1. In your dispose, you need to call dispose on the focus scope node:
      @override
      void dispose() {
        // The attachment will automatically be detached in dispose().
        _node.dispose();
        super.dispose();
      }
    1. In your build method, you will be calling reparent instead of reparentIfNeeded:
      @override
      Widget build(BuildContext context) {
        _nodeAttachment.reparent();
        return ...;
      }
    1. 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.

  4. AndreaMiotto commented on Apr 30, 2019

    @AndreaMiotto
    Author

    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.

  5. gspencergoog commented on Apr 30, 2019

    @gspencergoog
    Contributor

    You can use this: FocusScope.of(context).unfocus()

  6. github-actions commented on Aug 30, 2021

    @github-actions

    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.

  7. locked as resolved and limited conversation to collaborators on Aug 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions