Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 2 mixins in State in Flutter?

Tags:

flutter

dart

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.

like image 949
Dmitry Volkov Avatar asked Oct 16 '18 07:10

Dmitry Volkov


People also ask

How do you use mixin in Flutter?

To implement a mixin , create a class that extends Object and declares no constructors. Unless you want your mixin to be usable as a regular class, use the mixin keyword instead of class . In this way, your Car can run , but cannot be ' handleControlled '.

What is state mixin Flutter?

In Flutter, that means a setState() function is being called somewhere. The Dart package and its class, StateSet, will allow access to the State object previously instantiated back on the home screen — the very State object that built the home screen. That's huge!

Can we extend two classes in Flutter?

The class 'SearchDelegate' can't be used as a mixin because it extends a class other than Object. Because of calling the constructor with-param, it would be better to call a new class where you can implement SearchDelegate easily.

How do you inherit two classes in darts?

Dart inheritance Inheritance refers to a class's capability to derive properties and behaviors from another class. Dart allows one class to inherit from another, generating a new class from an existing one. We use the extend keyword to inherit from another class.


2 Answers

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

}
like image 83
Günter Zöchbauer Avatar answered Sep 30 '22 16:09

Günter Zöchbauer


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

like image 40
Chris Jensen Avatar answered Sep 30 '22 18:09

Chris Jensen