1

while animation was running i would like to go back to next page or previous page... but this error shows up saying "AnimationController.stop() called after AnimationController.dispose() AnimationController methods should not be used after calling dispose."

Please help me...

          import 'package:flutter/material.dart';
          import 'package:flutter_screenutil/flutter_screenutil.dart';
          import 'package:get/get.dart';
          import 'package:sanduk/utils/app_colors.dart';
          import 'package:sanduk/utils/text_widget.dart';
          import 'dart:math' as math;

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

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

          class _QuizSearchingPlayersState extends State<QuizSearchingPlayers>
              with SingleTickerProviderStateMixin {
            late AnimationController _controller;

            @override
            void initState() {
              super.initState();
              _controller = AnimationController(
                duration: const Duration(seconds: 2),
                vsync: this,
              )..repeat();

              _controller.addListener(() async {
                await stoppingAnimation();
              });
            }

            Future stoppingAnimation() async {
              await Future.delayed(const Duration(seconds: 10));
              _controller.reset();
              _controller.stop();
              return true;
            }

            @override
            void dispose() {
                _controller.dispose();
              super.dispose();
              
            
            }

            @override
            Widget build(BuildContext context) {
              return SafeArea(
                child: Scaffold(
                  backgroundColor: AppColors.darkThemeBackground,
                  body: Container(
                    height: Get.height,
                    width: Get.width,
                    padding: EdgeInsets.all(20.h),
                    child: Column(
                      children: [
                        TextWidget(
                          "BIOLOGY QUIZ",
                          styles: TextStyles.size18_500,
                          color: AppColors.white,
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10.w),
                          child: CircleAvatar(
                            radius: 150.r,
                            backgroundColor: AppColors.transparent,
                            child: AnimatedBuilder(
                              animation: _controller,
                              child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Stack(
                                  children: [
                                    Align(
                                      alignment: Alignment.center,
                                      child: CircleAvatar(
                                        radius: 120.r,
                                        backgroundColor: AppColors.blue,
                                        child: CircleAvatar(
                                          radius: 115.r,
                                          backgroundColor: AppColors.darkThemeBackground,
                                        ),
                                      ),
                                    ),
                                    Align(
                                      alignment: Alignment.centerLeft,
                                      child: CircleAvatar(
                                          radius: 40.r,
                                          backgroundColor: AppColors.red,
                                          backgroundImage: const AssetImage(
                                              "assets/images/sardar.png")),
                                    ),
                                    Align(
                                        alignment: Alignment.centerRight,
                                        child: CircleAvatar(
                                          radius: 40.r,
                                          backgroundColor: AppColors.darkBlue,
                                          backgroundImage: const AssetImage(
                                              "assets/images/sardar.png"),
                                        )),
                                  ],
                                ),
                              ),
                              builder: (context, child) {
                                return Transform.rotate(
                                  angle: _controller.value * 2 * math.pi,
                                  child: child,
                                );
                              },
                            ),
                          ),
                        ),
                        TextWidget(
                          "Searching For Opponent..",
                          styles: TextStyles.size32_400,
                          color: AppColors.blue,
                          maxLines: 2,
                        ),
                      ],
                    ),
                  ),
                ),
              );
            }
          }

Everything were working good till i navigate back to otherscreen.... and when i print some line inside my stoppingAnimation() method it keeps printing forever how do i stop that?

Peter Bk
  • 88
  • 1
  • 7
  • 1
    Solved by keeping my stoppingAnimation method oustide of addlistener method [code] @override void initState() { super.initState(); _controller = AnimationController( duration: const Duration(seconds: 2), vsync: this, )..repeat(); _controller.addListener(() { }); stoppingAnimation(); } Future stoppingAnimation() async { await Future.delayed(const Duration(seconds: 5)); // ignore: avoid_print print(_controller.status); _controller.reset(); _controller.stop(); return true; } – Peter Bk Nov 28 '21 at 04:11
  • you can write down it on answer section – Md. Yeasin Sheikh Nov 28 '21 at 05:57
  • thankyou @YeasinSheikh – Peter Bk Nov 29 '21 at 07:01

1 Answers1

2

Solved by keeping my stoppingAnimation method outside of addlistener method

           @override
          void initState() {
            super.initState();
            _controller = AnimationController(
              duration: const Duration(seconds: 2),
              vsync: this,
            )..repeat();

            _controller.addListener(() {
              if (_controller.status == AnimationStatus.dismissed) {
                setState(() {
                  opponentFound = true;
                  _quizAnimationController.countDownTogetReady();
                });
              }
            });

            stoppingAnimation();
          }

          Future stoppingAnimation() async {
            await Future.delayed(const Duration(seconds: 5));
            _controller.reset();
            _controller.stop();
          }

          @override
          void dispose() {
            _controller.dispose();
            super.dispose();
          }

not sure i think the _controller.addListener() method was running silently listening to the events occuring like "print statement" which was inside my stoppingAnimation() method. that's why the printing was occuring multiple times. so i just used stopingAnimation() method outside the _controller.addListener() method and worked fine.

i am new in this flutter community so not sure how things works.

Peter Bk
  • 88
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '21 at 07:02
  • yh, this only addresses your issue by chance it wont work generally, what we want is some way of knowing the controller is disposed, you could make the controller nullable and set it to null after disposing it, then check if its null before trying to use it – martinseal1987 Aug 13 '23 at 21:22