flutter_easyloading icon indicating copy to clipboard operation
flutter_easyloading copied to clipboard

Failed assertion: '_dependents.isEmpty': is not true.

Open daohoangson opened this issue 3 years ago • 1 comments

Describe the bug

If a stateful widget throws exception during didUpdateWidget, popping the Navigator will trigger the failed assertion.

Reproduce code.
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: EasyLoading.init(),
      onGenerateRoute: (_) => MaterialPageRoute(builder: (_) => const Page1()),
    );
  }
}

class Page1 extends StatelessWidget {
  const Page1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () => Navigator.of(context).push(
            MaterialPageRoute(builder: (_) => const Page2()),
          ),
          child: const Text('Start the test'),
        ),
      ),
    );
  }
}

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

  @override
  State<Page2> createState() => _Page2State();
}

class _Page2State extends State<Page2> {
  var shouldThrow = false;

  @override
  void initState() {
    super.initState();

    Future.delayed(const Duration(seconds: 1))
        .then((_) => setState(() => shouldThrow = true));
    Future.delayed(const Duration(seconds: 3))
        .then((_) => Navigator.pop(context));
  }

  @override
  Widget build(BuildContext context) {
    return _StatefulWidget(shouldThrow: shouldThrow);
  }
}

class _StatefulWidget extends StatefulWidget {
  final bool shouldThrow;

  const _StatefulWidget({Key? key, required this.shouldThrow})
      : super(key: key);

  @override
  State<_StatefulWidget> createState() => _State();
}

class _State extends State<_StatefulWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(child: Text('shouldThrow=${widget.shouldThrow}')),
    );
  }

  @override
  void initState() {
    super.initState();
    _maybeThrow();
  }

  @override
  void didUpdateWidget(covariant _StatefulWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    _maybeThrow();
  }

  void _maybeThrow() {
    if (widget.shouldThrow) {
      throw 'This exception is expected.';
    }
  }
}

Flutter/Dart info

[✓] Flutter (Channel stable, 2.10.0, on macOS 11.3 20E232 darwin-x64, locale en)
    • Flutter version 2.10.0 at /Users/xxx/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5f105a6ca7 (3 weeks ago), 2022-02-01 14:15:42 -0800
    • Engine revision 776efd2034
    • Dart version 2.16.0
    • DevTools version 2.9.2

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/xxx/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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.10+0-b96-7281165)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.64.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.34.0

[✓] Connected device (3 available)
    • iPhone 13 (mobile) • 5FC7388A-59D8-4476-896A-2D101BD04108 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-2 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64     • macOS 11.3 20E232 darwin-x64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 98.0.4758.109

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

Screenshots

The demo throws this on purpose 👇 But Navigator.pop triggers this 👇
Simulator Screen Shot - iPhone 13 - 2022-02-23 at 17 43 13 Simulator Screen Shot - iPhone 13 - 2022-02-23 at 17 43 15

daohoangson avatar Feb 23 '22 10:02 daohoangson

Thanks for taking the time to open an issue. I will have a look and answer as soon as possible.

github-actions[bot] avatar Feb 23 '22 10:02 github-actions[bot]