23

I have a state class

class ListScreenState extends State<ListScreen>...

And I want to use AutomaticKeepAliveClientMixin (to prevent the TabBar that holds these screens from disposing of) and TickerProviderStateMixin because I have the animation controller that requires it. But when I put both mixins in this class there's an error:

error: Type parameters could not be inferred for the mixin 'TickerProviderStateMixin' because the base class implements the mixin's supertype constraint 'State<T>' in multiple conflicting ways (mixin_inference_inconsistent_matching_classes at [myapp] lib/trips/ListScreen.dart:21)

I couldn't really find a good explanation of how to use mixins in one class. Any help is appreciated.

Here's the full code:

import 'package:flutter/widgets.dart';    

class ListScreen extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return ListScreenState();
  }
}

 class ListScreenState extends State<ListScreen>
    with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {

  AnimationController controller;

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

    controller = AnimationController(
        duration: const Duration(milliseconds: 250), vsync: this);

  }

  @override
  bool get wantKeepAlive => true;
}

Dart specifically complains about adding TickerProviderStateMixin. If I remove AutomaticKeepAliveClientMixin, then it doesn't complain anymore.

Hamed
  • 5,867
  • 4
  • 32
  • 56
Dmitry Volkov
  • 500
  • 1
  • 3
  • 14

2 Answers2

47
class ListScreenState extends State<ListScreen> with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {
  // TODO: implement wantKeepAlive
  @override
  bool get wantKeepAlive => null;

}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
18

For anyone coming here using SingleTickerProviderStateMixin, simply remove the Single from the name.

Chris Jensen
  • 485
  • 3
  • 14