Flutter将变量传递给SearchDelegate类会给出错误

时间:2019-07-09 10:02:10

标签: search flutter dart flutter-dependencies

我是个新手,我无法将数据传递给搜索委托类。问题是我有两个标签,并且要在活动标签中搜索。因此,我尝试发送一个变量,该变量告诉它是哪个选项卡以及要查找值的表。

这是我的代码:

C:\WORKING\todo\todos-manager>sequelize db:migrate

Sequelize CLI [Node: 10.16.0, CLI: 5.5.0, ORM: 5.8.12]

Loaded configuration file "config\config.json".
Using environment "development".

ERROR: Please install sqlite3 package manually

当我尝试获取$ activeTab并在查询或其他地方显示它时,它只会发出错误:

    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),
                  ),
                );
              }
            }

我有点困惑我该如何传递价值。我已经看到一些类似的问题,但是它们没有帮助。像this或这个question。这些都没有任何这些错误。您能告诉我我做错了吗?有什么问题?请帮忙。

1 个答案:

答案 0 :(得分:0)

好吧,对于一个像我一样愚蠢并且遇到与我相同的问题的人,这是解决问题的方法, 因此,我没有将正确的值传递给Search委托,也没有正确地获取它。这是代码的固定部分

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

  final activeTab;

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