Null check operator used on a null value

Issue #35 resolved
xsm123 created an issue

when i update to the 1.0.1+1

1 need physics: BouncingScrollPhysics() else throw error

 TreeView(
                    key: _treeKey,
                    controller: _treeViewController,
                    allowParentSelect: true,
                    supportParentDoubleTap: false,
                    physics: BouncingScrollPhysics(),
                    onExpansionChanged: _expandNodeHandler,
                    onNodeTap: _onNodeTap,
                    onNodeDoubleTap: (key) {},
                    nodeBuilder: builder,
                    theme: treeViewTheme)

2 when click node ,throw error

  Node getNode(String key, {Node? parent}) {
    Node? _found;
    List<Node> _children = parent == null ? this.children : parent.children;
    Iterator iter = _children.iterator;
    while (iter.moveNext()) {
      Node child = iter.current;
      if (child.key == key) {
        _found = child;
        break;
      } else {
        if (child.isParent) {
          _found = this.getNode(key, parent: child);
          if (_found != null) {
            break;
          }
        }
      }
    }
    return _found!;
  }

look the code,find that when can't find a node by key , return _found!; will throw error --- Null check operator used on a null value

Fix

  Node? getNode(String key, {Node? parent}) {
    Node? _found;
    List<Node> _children = parent == null ? this.children : parent.children;
    Iterator iter = _children.iterator;
    while (iter.moveNext()) {
      Node child = iter.current;
      if (child.key == key) {
        _found = child;
        break;
      } else {
        if (child.isParent) {
          _found = this.getNode(key, parent: child);
          if (_found != null) {
            break;
          }
        }
      }
    }
    return _found;
  }

and some where use getNode()

Node _node = _this.getNode(k);

fix to

Node _node = _this.getNode(k)!;

Comments (7)

  1. xsm123 reporter

    need physics: BouncingScrollPhysics() else throw error

    It can't be reproduced. Maybe it's my environment fault

  2. Kevin Armstrong

    @xsm123 Thanks for the suggestion on the null check. I made that change and published them. I’m not sure what’s happening with the physics property in your app. The property is optional. Let me know if you continue to have issues.

  3. Mac Spinks

    I am still getting the same error if I don’t include ‘physics’. I traced it down to the following line

    Getting past that, I passed in a ScrollPhysics object and, using the example on the front page, it returns an empty list. Using v 1.0.3+1 and flutter stable 2.0.3.

  4. Kevin Armstrong

    @Mac Spinks I was able to reproduce it. I just fixed the issue and published the new version. Hopefully that addresses all null safety related issues.

  5. Mac Spinks

    Kevin, thank you for the response. That fixed the issue. Ignore my mention of the other issue. That was my fault

  6. Log in to comment