Md. Jahangir Alam
Md. Jahangir Alam

Reputation: 381

How can I extend two classes in flutter?

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{}

Upvotes: 27

Views: 39039

Answers (1)

Mimu Saha Tishan
Mimu Saha Tishan

Reputation: 2623

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

Upvotes: 42

Related Questions