gpt4 book ai didi

search - flutter 将变量传递给 SearchDelegate 类给出错误

转载 作者:行者123 更新时间:2023-12-03 03:40:21 25 4
gpt4 key购买 nike

我是一个新手,我在将数据传递给我的搜索委托(delegate)类时遇到问题。问题是我有两个标签,我想在事件标签中搜索。所以我试图发送一个变量,告诉它是哪个选项卡以及要查找哪个表的值。

这是我的代码的样子:

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

当我尝试获取 $activeTab 并在查询或其他地方显示它时,它只会给出错误:
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

我有点困惑我应该如何将值(value)传递给它。我见过一些类似的问题,但它们没有帮助。点赞 this或者这个 question .这些都没有任何这些错误。你能告诉我我做错了什么吗?什么问题?请帮忙。

最佳答案

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

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

final activeTab;

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




关于search - flutter 将变量传递给 SearchDelegate 类给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56950102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com