Flutter Error :- AnimationController methods should not be used after calling dispose

Viewed 942

Animation controller error when switch screen in bottom tab bar or route to another screen in flutter.

[ERROR:flutter/lib/ui/ui_dart_state.cc(171)] Unhandled Exception: 'package:flutter/src/animation/animation_controller.dart': Failed assertion:

'_ticker != null': AnimationController.stop() called after AnimationController.dispose()

AnimationController methods should not be used after calling dispose.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:loading/indicator/ball_beat_indicator.dart';
import 'package:loading/loading.dart';

import '../models/Category.dart';
import './category_articles.dart';
import '../repo/dbRepo.dart';
import './sideDrawer.dart';

class Categories extends StatefulWidget {
  const Categories({Key key}) : super(key: key);
  @override
  _CategoriesState createState() => _CategoriesState();
}

class _CategoriesState extends State<Categories> {
  Future<List<dynamic>> _futureCategories;
  List<dynamic> categories = [];
  final repo = DbRepo();

  @override
  void initState() {
    super.initState();
    _futureCategories = fetchCategories();
  }

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

  Future<List<dynamic>> fetchCategories() async {
    try {
      var response = await repo.catList();

      if (this.mounted) {
        setState(() {
          categories = response.map((m) => Category.fromJson(m)).toList();
        });
        print(categories);
        return categories;
      }
    } on SocketException {
      throw 'No Internet connection';
    }
    return categories;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: SideDrawer(),
      appBar: AppBar(
        centerTitle: true,
        title: Text('Categories',
            style: TextStyle(
                color: Colors.black,
                fontWeight: FontWeight.bold,
                fontSize: 20,
                fontFamily: 'Poppins')),
        elevation: 5,
        backgroundColor: Colors.white,
        iconTheme: new IconThemeData(color: Colors.blue),
      ),
      body: Container(
        decoration: BoxDecoration(color: Colors.white),
        child: getCategoriesList(_futureCategories),
      ),
    );
  }

  Widget getCategoriesList(Future<List<dynamic>> categories) {
    return FutureBuilder<List<dynamic>>(
      future: categories,
      builder: (context, categorySnapshot) {
        if (categorySnapshot.hasData) {
          if (categorySnapshot.data.length == 0) return Container();
          return GridView.builder(
              itemCount: categorySnapshot.data.length,
              padding: EdgeInsets.all(10),
              gridDelegate:
                  SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
              itemBuilder: (BuildContext context, int index) {
                Category category = categorySnapshot.data[index];

                return InkWell(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => CategoryArticles(
                            id: category.id, name: category.name),
                      ),
                    );
                  },
                  child: Card(
                    elevation: 8,
                    shadowColor: Colors.amber,
                    margin: EdgeInsets.all(10),
                    child: Center(
                      child: Padding(
                        padding:
                            EdgeInsets.symmetric(vertical: 20, horizontal: 4),
                        child: Text(
                          category.name,
                          style: TextStyle(
                            fontWeight: FontWeight.w700,
                            fontSize: 18,
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              });
        } else if (categorySnapshot.hasError) {
          return Container();
        }
        return Container(
            alignment: Alignment.center,
            child: Loading(
                indicator: BallBeatIndicator(),
                size: 60.0,
                color: Theme.of(context).accentColor));
      },
    );
  }
}
0 Answers
Related