Flutter pass variable to SearchDelegate class gives error

0

I am a newbie to flutter, I am having a problem with passing the data to my search delegate class. The problem is that I have two tabs and I want to search within the active tab. So I am trying to send a variable that tells which tab is it and which table to look for value.

Here is what my code looks like:

    class HomePage extends StatefulWidget {
                  static final String routeName = 'home';

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

                class _HomePageState extends State<HomePage> with TickerProviderStateMixin {

                  var activeTab = "activity";

                  var _authToken, _id, _name, _emails, _userImage;

                  @override
                  void initState() {
                    super.initState();
                    tabController = TabController(vsync: this, length: 2)..addListener(() {
                        setState(() {
                          switch (tabController.index) {
                            case 0:
                              activeTab = "activity";
                              break;
                            case 1:
                              activeTab = "subparticipants";
                              break;
                          }
                        });
                      });
                  }



                  @override
                  Widget build(BuildContext context) {
                    return new Scaffold(
                      key: _scaffoldKey,
                      // appBar: new AppBar(
                      //   title: Text('Dashboard'),
                      // ),
                      body: DefaultTabController(
                        length: 2,
                        child: Scaffold(
                          appBar: AppBar(
                            bottom: TabBar(
                              labelColor: Color(0xFFFFFFFF),
                              indicatorSize: TabBarIndicatorSize.tab,
                              tabs: [
                                //Tab(icon: Icon(Icons.directions_car)),
                                Tab(
                                  text: "Activity Zone",
                                ),
                                Tab(
                                  text: "Sub Participant",
                                )
                              ],
                              controller: tabController,
                            ),
                            title: Text(
                              'Dashboard',
                              style: new TextStyle(
                                color: const Color(0xFFFFFFFF),
                                fontSize: 20.0,
                                fontWeight: FontWeight.w600,
                                letterSpacing: 0.3,
                              ),
                            ),
                            actions: <Widget>[
                              IconButton(
                                icon: Icon(Icons.search),
                                onPressed: () {
                                  showSearch(context: context, delegate: DataSearch(activeTab));
                                },
                              )
                            ],
                          ),
                          body: TabBarView(
                            controller: tabController,
                            children: [
                              TabActivity(),
                              TabSubparticipant(),
                            ],
                          ),
                          floatingActionButton: FloatingActionButton(
                              onPressed: () {
                                print(
                                    'Current Index: $activeTab');
                              },
                            ),
                          drawer: _buildDrawer(context),
                        ),
                      ),
                    );
                  }
                }
     class DataSearch extends SearchDelegate{
              final String activeTab;
              DataSearch(this.activeTab);

              @override
              List<Widget> buildActions(BuildContext context){
                return [
                  IconButton(
                    icon: Icon(Icons.arrow_back),
                    onPressed: (){
                      query=activeTab;
                    },
                  )
                ];
              }

              @override
              Widget buildLeading(BuildContext context) => IconButton(
                    icon: Icon(Icons.close),
                    onPressed: () => Navigator.of(context).pop(),
                  );

              @override
              Widget buildResults(BuildContext context) => Text('Result');

              @override
              Widget buildSuggestions(BuildContext context) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    'Search by job id, asset name, client name $query',
                    style: new TextStyle(
                        color: Colors.black, fontWeight: FontWeight.bold, fontSize: 22.0),
                  ),
                );
              }
            }

When I try to get $activeTab and show it in query or somewhere else, It just gives out the error:

flutter: The following assertion was thrown building _SearchPage<dynamic>(dirty, dependencies:
flutter: [_LocalizationsScope-[GlobalKey#a02e3], _InheritedTheme], state: _SearchPageState<dynamic>#eceaa):
flutter: 'package:flutter/src/widgets/basic.dart': Failed assertion: line 6173 pos 15: 'child != null': is

I am a bit confused how should I pass value to it. I have seen some of similar questions but they are no help. Like this or this question. None of these have any of these errors. Can you please let me know what am I doing wrong. Whats the issue? Please help.

search
flutter
dart
flutter-dependencies
asked on Stack Overflow Jul 9, 2019 by Faran Khan

1 Answer

0

Well, For someone who dumb as me and is having the same problem as I am, Here is how you can fix the issue, So I was not passing the correct value to Search delegate and was not picking it up properly. Here is the fixed part of code

   class DataSearch extends SearchDelegate {
      DataSearch({
        @required this.activeTab,
      });

  final activeTab;

@override
  Widget buildResults(BuildContext context) {
        if (activeTab == "subparticipants") {
...... .
....
..




answered on Stack Overflow Jul 11, 2019 by Faran Khan

User contributions licensed under CC BY-SA 3.0