Skip to content

Conversation

@LongCatIsLooong
Copy link
Contributor

@LongCatIsLooong LongCatIsLooong commented Jun 11, 2019

Description

Calling insertChildRenderObject and removeChildRenderObject outside of update or mount in _TableElement should be allowed.

Consider the following minimal reproducible example:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Center(
        child: SizedBox(
          width: 100,
          height: 100,
          child: Table(children: [TableRow(children: [W()])]),
        ),
      ),
    ),
  );
}

class W extends StatefulWidget {
  const W({ Key key }) : super(key: key);

  @override
  _State createState() => _State();
}

class _State extends State<W> {
  bool _toggle = true;
  @override
  Widget build(BuildContext context) {
    return _toggle
        ? GestureDetector(onTap: () => setState(() => _toggle = !_toggle), child: Text('Tap me'))
        : SizedBox();
  }
}

Sequence of events after tapping 'Tap me'

  1. The onTap callback is called, _State.context is marked dirty.
  2. In the next build pass, because GestureDetector was replaced by a widget of a different type, _State.context.updateChild method calls _State.context.deactivateChild.
  3. The child's renderObject asks _TableElement to detach it from _TableElement.renderObject, calling _TableElement.removeChildRenderObject.
  4. The assertion in _TableElement.removeChildRenderObject fails.

In the above call path only removeChildRenderObject was called on a _TableElement instance, pretty much everything else is happening in the descendant elements which we have no knowledge of in the implementation of Table. So it's probably easier to just remove the asserts than figuring out a way to keep them.

Related PR

_debugWillReattachChildren wasn't brought up in #2961's discussion.

Related Issues

Fixes #31473

Tests

I added the following tests:

  • 'Does not crash if a child RenderObject is replaced by another RenderObject of a different type',

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require Flutter developers to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (Please read Handling breaking changes). Replace this with a link to the e-mail where you asked for input on this proposed change.
  • No, this is not a breaking change.

@LongCatIsLooong LongCatIsLooong added the framework flutter/packages/flutter repository. See also f: labels. label Jun 11, 2019
@Piinks Piinks added customer: payouts f: material design flutter/packages/flutter/material repository. labels Jun 12, 2019
@LongCatIsLooong LongCatIsLooong removed the f: material design flutter/packages/flutter/material repository. label Jul 2, 2019
Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you checkin with @Hixie, who wrote the original code, to figure out if he remembers why the assert was added in the first place? There may have been a good reason for them to be there in the first place.

@override
Widget build(BuildContext context) {
return toggle
? const SizedBox()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indentation is off here (this could probably just all be in one line)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trivial nit: no space between > and [

@Hixie
Copy link
Contributor

Hixie commented Jul 11, 2019

LGTM

yeah i think this assert is just bogus because the table doesn't necessarily know if some widget below it gets removed and readded with another type.

@LongCatIsLooong LongCatIsLooong merged commit 539f09f into flutter:master Jul 11, 2019
@LongCatIsLooong LongCatIsLooong deleted the remove-table-asserts branch July 11, 2019 20:54
johnsonmh pushed a commit to johnsonmh/flutter that referenced this pull request Jul 30, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table widget doesn't work with Hero animations.

5 participants