Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extend two classes in flutter?

Tags:

flutter

dart

I am trying to extend two classes having with keyword in flutter to get SearchDelegate override methods but can't do so. Please suggest any solution.

class A extends StatefulWidget with SearchDelegate{}
like image 799
Md. Jahangir Alam Avatar asked Sep 08 '25 07:09

Md. Jahangir Alam


1 Answers

Normally, you can easily call multiple classes (inheritance) in dart using the 'with' keyword, like

Class A extends B with C, D {
    //your code
}

But when u call SearchDelegate you may see

The class 'SearchDelegate' can't be used as a mixin because it declares a constructor.

The class 'SearchDelegate' can't be used as a mixin because it extends a class other than Object.

Short Explanation: Here, u have to override all its abstract methods, as well as you need to pass a String/int variable in your declared class. That means, you might have to call DataSearch class constructor with the needed variable

Because of calling the constructor with-param, it would be better to call a new class where you can implement SearchDelegate easily.

Like for any button action

onPressed: () {
        showSearch(
          context: context,
          delegate: CustomSearchDelegate(),
        );
      },

I hope, my answer make a sense to you. Thanks

like image 57
Mimu Saha Tishan Avatar answered Sep 10 '25 16:09

Mimu Saha Tishan