Repository: flutter
Issue: Cannot change Sliver Child (center!.parent == this)
Labels: framework,f: scrolling,has reproducible steps,P2,found in release: 2.2,found in release: 2.3,team-framework,triaged-framework

When trying to return a different `Sliver` child to a `CustomScrollView`, the following exception is thrown:

```
══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞══════════════════════
The following assertion was thrown during a scheduler callback:
'package:flutter/src/rendering/viewport.dart': Failed assertion:
line 1696 pos 12: 'center!.parent == this': is not true.
```

I noticed this in a real application that changes the sliver child based on the loading state via `riverpod`. I was also able to create a minimal reproducible sample.

## Steps to Reproduce

1. Run `flutter create bug`.
2. Add `flutter_riverpod: 1.0.0-dev.1` as a dependency.
3. Change `main.dart` to the following:

<details><summary>Click to expand code snippet</summary>

```dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() =>
    runApp(ProviderScope(child: MaterialApp(home: Scaffold(body: Bug()))));

final list = StateProvider<bool>((ref) => false);

class Bug extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    FlutterError.onError = (e) {
      print(e.toString());
    };

    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        ref.read(list).state = !ref.read(list).state;
      },
      child: SizedBox.expand(
        child: CustomScrollView(
          slivers: [BugSliver()],
        ),
      ),
    );
  }
}

class BugSliver extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    Widget sliver;

    if (ref.watch(list).state) {
      sliver = SliverPadding(
        padding: const EdgeInsets.all(9),
        sliver: SliverList(
          delegate: SliverChildBuilderDelegate(
            (context, index) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text('index: $index'),
              );
            },
          ),
        ),
      );
    } else {
      sliver = const SliverFillRemaining(
        child: Center(
          child: Text('center'),
        ),
      );
    }

    return sliver;
  }
}
```

</details>

4. `flutter run`
5. Tap on the screen.

### Behavior

**Expected results:** the screen should switch between `center` and an infinite list.

**Actual results:** the above error is printed to the console.

<details>
  <summary>Click to expand full logs</summary>


```
I/flutter ( 7705): ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞══════════════════════
I/flutter ( 7705): The following assertion was thrown during a scheduler callback:
I/flutter ( 7705): 'package:flutter/src/rendering/viewport.dart': Failed assertion:
I/flutter ( 7705): line 1696 pos 12: 'center!.parent == this': is not true.
I/flutter ( 7705): 
I/flutter ( 7705): Either the assertion indicates an error in the framework itself,
I/flutter ( 7705): or we should provide substantially more information in this error
I/flutter ( 7705): message to help you determine and fix the underlying cause.
I/flutter ( 7705): In either case, please report this assertion by filing a bug on
I/flutter ( 7705): GitHub:
I/flutter ( 7705):   https://github.com/flutter/flutter/issues/new?template=2_bug.md
I/flutter ( 7705): 
I/flutter ( 7705): When the exception was thrown, this was the stack:
I/flutter ( 7705): #2      RenderViewport.indexOfFirstChild (package:flutter/src/rendering/viewport.dart:1696:12)
I/flutter ( 7705): #3      RenderViewportBase.debugDescribeChildren (package:flutter/src/rendering/viewport.dart:968:17)
I/flutter ( 7705): #4      DiagnosticableTreeNode.getChildren (package:flutter/src/foundation/diagnostics.dart:2994:48)
I/flutter ( 7705): #5      TextTreeRenderer._debugRender (
```

</details>

### Versions

<details>
  <summary>Click to expand Flutter version information</summary>


```
[✓] Flutter (Channel stable, 2.2.2, on Microsoft Windows [Version 10.0.19041.1052], locale en-US)
    • Flutter version 2.2.2
    • Framework revision d79295af24 (11 days ago), 2021-06-11 08:56:01 -0700
    • Engine revision 91c9fc8fe0
    • Dart version 2.13.3
```

</details>

## Workaround

I looked into the root cause of the issue and I figured that it actually has to do with the child `RenderSliver` changing (because the parent viewport is incorrect).

I solved the issue by changing:

```dart
Widget sliver;
if (..) {
  sliver = ..;
} else {
  sliver = ..;
}
return sliver;
```

To:

```dart
Widget sliver;
if (..) {
  sliver = ..;
} else {
  sliver = ..;
}
return SliverOpacity(
  opacity: 1,
  sliver: sliver,
);
```

This is a **hack** because it ensures that the `RenderSliver` stays the same but passing an `opacity` of `1` does not change anything visually. It is like wrapping in a bare `Container` in the box contract.