Flutter : AnimationController.stop() called after AnimationController.dispose() AnimationController methods should not be used after calling dispose.)

4k views Asked by At

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?

1

There are 1 answers

2
Peter Bk On

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.