23

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{}
MRUNAL MUNOT
  • 395
  • 1
  • 5
  • 18
Md. Jahangir Alam
  • 361
  • 1
  • 2
  • 9

1 Answers1

37

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

Mimu Saha Tishan
  • 2,402
  • 1
  • 21
  • 40