Search code examples
flutterrotationstateback

Error after rotating screen on Flutter. Dependets is Empty


I have an issue. When I navigate to a screen and rotate to landscape to reproduce a video, and then I go back to a portrait screen, I get this error:

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 5729 pos 14: '_dependents.isEmpty': is not true.

And if I remove the rotation, the error doesn't occur.

Here is the page code:

class VideoPage extends StatefulWidget {
  final String title;
  final String url;
  final bool portrait;

  const VideoPage({
    Key? key,
    required this.title,
    required this.url,
    this.portrait = false,
  }) : super(key: key);

  @override
  State<VideoPage> createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  late bool isPortrait;

  @override
  void initState() {
    super.initState();
    if (widget.portrait) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
    } else {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
      ]);
    }
  }

  @override
  void dispose() {
    super.dispose();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
      appBar: AppBar(
          actions: [
            TextButton.icon(
                label: const Icon(
                  FontAwesomeIcons.solidEarthAmericas,
                  color: Colors.white,
                ),
                onPressed: () => launchUrlString(widget.url),
                icon: Text(
                  'openintheweb_video'.tr,
                  style: const TextStyle(color: Colors.white),
                )),
          ],
          title: Text('video'.tr),
          leading: IconButton(
              splashRadius: 10.sp,
              onPressed: () => Get.back(),
              icon: const Icon(FontAwesomeIcons.solidChevronLeft))),
      body: SizedBox(width: Get.width, child: VideoPlayer(url: widget.url)),
    );
  }
}

As u can see, i use the Get.back() to navigate to my previous page, but i used the navigator and still throwing this error.


Solution

  • try this in previous screen.

           if (condition) 
               SystemChrome.setPreferredOrientations([
               DeviceOrientation.landscapeRight,
               DeviceOrientation.landscapeLeft,
            ]);
    
            await Get.to(VideoPage());
    
            SystemChrome.setPreferredOrientations([
               DeviceOrientation.portraitUp,
               DeviceOrientation.portraitDown,
            ]);