Repository: flutter
Issue: Draggable scrollable controller is already attached to a sheet
Labels: framework,a: error message,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.16,found in release: 3.18

### Steps to reproduce

Toggle widget rendering using `setState` and `if`.

### Expected results

Toggle widget rendering normally.

### Actual results

Toggle widget rendering with an error.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'dart:ui';

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
      scrollBehavior: const MaterialScrollBehavior().copyWith(
        dragDevices: PointerDeviceKind.values.toSet(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _flag = false;
  final _draggableScrollableController = DraggableScrollableController();

  @override
  void dispose() {
    _draggableScrollableController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(children: [
        if (_flag) const Flexible(child: Placeholder()),
        Flexible(
          child: DraggableScrollableSheet(
            // key: const GlobalObjectKey('DraggableScrollableSheet'),
            controller: _draggableScrollableController,
            builder: (context, scrollController) => ListView.builder(
              itemCount: 100,
              controller: scrollController,
              itemBuilder: (context, index) => ListTile(
                title: Text('Item $index'),
              ),
            ),
          ),
        ),
      ]),
      floatingActionButton: FloatingActionButton(
        mini: true,
        onPressed: () => setState(() => _flag = !_flag),
        child: const Icon(Icons.compare),
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

![DraggableScrollableController](https://github.com/flutter/flutter/assets/38378650/08728094-055d-4ddb-b9f5-8aba1719e0e0)

</details>


### Logs

<details open><summary>Logs</summary>

```console
======== Exception caught by widgets library =======================================================
The following assertion was thrown building Flexible(flex: 1):
Draggable scrollable controller is already attached to a sheet.
'package:flutter/src/widgets/draggable_scrollable_sheet.dart':
Failed assertion: line 185 pos 12: '_attachedController == null'
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.13.9, on Microsoft Windows [版本 10.0.19045.3570], locale zh-CN)
• Flutter version 3.13.9 on channel stable at D:\Temporary\flutter\_flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision d211f42860 (9 weeks ago), 2023-10-25 13:42:25 -0700
• Engine revision 0545f8705d
• Dart version 3.1.5
• DevTools version 2.25.0

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at C:\Users\Flop\AppData\Local\Android\sdk
• Platform android-34, build-tools 33.0.0
• Java binary at:
C:\Users\Flop\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\222.4459.24.2221.9862592\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
• All Android licenses accepted.

[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.18)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.11.32802.440
• Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2021.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[√] Android Studio (version 2022.2)
• Android Studio at C:\Users\Flop\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\222.4459.24.2221.9862592
• Flutter plugin version 76.0.1
• Dart plugin version 222.4582
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] VS Code, 64-bit edition (version 1.76.2)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension can be installed from:
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64    • Microsoft Windows [版本 10.0.19045.3570]
• Chrome (web)      • chrome  • web-javascript • Google Chrome 119.0.6045.200
• Edge (web)        • edge    • web-javascript • Microsoft Edge 120.0.2210.91

[√] Network resources
• All expected network resources are available.

• No issues found!
```

</details>