我決定不使用searchdelegate,而是使用JSON中的常規搜索(因為我需要在近2小時內解決這個問題)。問題是,一切似乎都作業得很好(我在堆疊跟蹤中沒有錯誤),但我就是不明白為什么搜索不作業。我決定有必要在搜索部件內使用 "未來",但這也沒有得到任何結果。誰能給我指出到底是哪里出了問題?也許我需要創建一個陣列來添加搜索結果?我使用的不僅僅是一個普通的JSON,而是要經過一個排序演算法,我在其他的解決方案中看到,人們使用一個元素的串列,然后從所有的元素中獲取一個元素:像這樣--Future <List<Post>> fetchAllPosts 和 Future<Post> fetchPost。但我是這樣做的:
class MarshrutesPage extends StatefulWidget {
final int ttId;
MarshrutesPage({this.ttId})。
@override。
_MarshrutesPageState createState() => _MarshrutesPageState()。
}
class _MarshrutesPageState extends State< MarshrutesPage> {
Box<RouteWithStops> favoriteRoutesBox;
TransportService service = getIt<TransportService>()。
@override
void initState() {
super.initState()。
favoriteRoutesBox = Hive.box(favoritesBox);
}
@override.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
行動。[
IconButton(
圖示。Icon(
Icons.search,
color: Colors.white,
),
onPressed: () {
showSearch(context: 背景關系, delegate: SearchBar())。
},
),
IconButton(
圖示。Icon(
Icons.favorite,
color: Colors.white,
),
onPressed: () {
Navigator.pushNamed(context, 'favorite') 。
},
),
],
提升。0.0。
backgroundColor: Colors.green,
標題。文本(
'numbers',
風格。GoogleFonts.montserrat(
color: Colors.white, fontWeight: FontWeight.w400)。)
),
),
身體。FutureBuilder(
future: service.getMarshrutWithStops(widget.ttId),
builder: ( BuildContext context, AsyncSnapshot<dynamic> snapshot) {
List<RouteWithStops> routes = snapshot.data;
print(routes?.toString())。
return (routes == null)
? Center(child: CircularProgressIndicator())
: ValueListenableBuilder()
valueListenable: favoriteRoutesBox.listenable(),
builder: (BuildContext context, value, Widget child) {
return ListView.builder(
itemCount: routes.length 1,
itemBuilder。(context, index) {
return index == 0 ?
? _searchBar()
: _listItem(index - 1, routes)。
},
);
},
);
}),
bottomNavigationBar: BottomAppBar(
孩子。行(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
圖示。Icon(
Icons.message,
color: Colors.gray,
),
tooltip: 'to devs'。
onPressed: () async {
await launch("mailto: @gmail.com") 。
}),
IconButton(
圖示。Icon(Icons.home, color: Colors.gray), onPressed: () {}),
IconButton(
圖示。Icon(Icons.notifications, color: Colors.gray),
onPressed: () {}),
IconButton(
圖示。Icon(Icons.map, color: Colors.gray), onPressed: () {}),
]),
),
);
}
Widget getIcon(int index) {
if (favoriteRoutesBox.containsKey(index)) {
return Icon(Icons.favorite, color: Colors.red)。
}
return Icon(Icons.favorite_border)。
}
void onFavoritePress(int index) {
List< RouteWithStops> routes;
if (favoriteRoutesBox.containsKey(index)) {
favoriteRoutesBox.delete(index)。
return;
}
favoriteRoutesBox.put(index, routes[index])。
}
_listItem(index, List<RouteWithStops> routes) {
return ListTile(
標題。Text(route[index].route.mrTitle)。
領導。Text(
routes[index].route.mrNum,
風格。TextStyle(color: Colors.green, fontSize: 20) 。
),
后面的。IconButton(
icon: getIcon(index),
onPressed: () => onFavoritePress(index),
),
onTap: () {
Navigator.push(
背景關系。
MaterialPageRoute(
builder: (context) => StopsPage(
routeWithStops: routes[index],
)));
},
);
}
_searchBar() {
return FutureBuilder(
future: service.getMarshrutWithStops(widget.ttId),
builder:
(BuildContext context, AsyncSnapshot<List<RouteWithStops> > snapshot) {
List<RouteWithStops> routes = snapshot.data;
print('test1')。
return (rouse == null)
? Center(child: CircularProgressIndicator()) 。Padding()
padding: const EdgeInsets.all(8) 。
孩子。TextField(
裝飾。InputDecoration(hintText: 'Search'。
hoverColor: Colors.green,
邊界。輪廓輸入邊框()
borderRadius: borderRadius.round(10)。
),
prefixIcon: Icon(Icons.search),)。
onChanged: (text) {
text = text.toLowerCase();
setState(() {
routes = routes.where((element) {
var routesTitle = element.route.mrTitle.toLowerCase()。
return routesTitle.contains(text);
}).toList()。
print('test2')。
});
},
),
);
},
);
}
}
此外,我認為它不作業,因為我使用的是引數--ttId.
。我使用的演算法
Future<List< RouteWithStops>> getMarshrutWithStops(int ttId) async {
if (rousebyTransportType.isEmpty) {
await fetchTransportWithRoutes()。
}
List<Routes> routes = routesbyTransportType[ttId].rouse;
List<ScheduleVariants> variants = [] 。
variants.addAll(await api.fetchSchedule())。
List<RouteWithStops> routesWithStops = [] 。
for (Routes route in routes) {
final routeWithStops = RouteWithStops();
routesWithStops.add(routeWithStops)。
routeWithStops.route=路由。
routeWithStops.variant =
variants.where((variant) => variant.mrId == route.mrId).first。
}
return routesWithStops。
}
在搜索欄中放入幾個字母后的堆疊跟蹤:
W/IInputConnectionWrapper(25362): beginBatchEdit on notactive InputConnection
W/IInputConnectionWrapper(25362): endBatchEdit on inactive InputConnection
I/flutter(25362)。[Instance of 'RouteWithStops', Instance of 'RouteWithStops', Instance of 'RouteWithStops', 'RouteWithStops'的實體, 'RouteWithStops'的實體, 'RouteWithStops'的實體, 'RouteWithStops'的實體, 'RouteWithStops'的實體。'RouteWithStops'的實體, 'RouteWithStops'的實體, 'RouteWithStops'的實體, 'RouteWithStops'的實體, 'RouteWithStops'的實體。Instance of 'RouteWithStops', Instance of 'RouteWithStops', Instance of 'RouteWithStops']
I/flutter(25362):test2
I/flutter(25362)。Basic Vk9MR0E6TkVU
uj5u.com熱心網友回復:
routes = routes.where((element) {
這是在設定你的本地變數,名為 "routes"。它不會做任何事情。我真的不知道你想做什么,也許是設定一個你的狀態變數?
uj5u.com熱心網友回復:
這個問題的解決很簡單,我只是需要更小心一些:一般來說,我需要在widget樹本身的頂部宣告兩個陣列:
List<RouteWithStops> _routes = [] 。
List<RouteWithStops> _routesToDisplay = [];
第一個陣列負責所有需要在搜索欄下顯示的串列元素。第二個陣列是需要的,這樣當你設定搜索并在那里輸入某個名稱/值時,那么它就不會只留下你剛剛輸入的值,而是同時加載其他的值。例如,你正在按作者尋找一個標題。托爾斯泰《戰爭與和平》。如果你讓它保持原樣,不添加第二個陣列,那么在下一次重新加載之前,搜索欄下的托爾斯泰《戰爭與和平》仍然是唯一的專案。它不一定是這樣的。
為了避免這種情況,你需要做以下作業:
@override
void initState() {
service.getMarshrutWithStops(widget.ttId).then((value) {
setState(() {
_routes.addAll(value);
_routesToDisplay = _routes;
});
});
super.initState()。
}
第一個陣列中的資料被寫入空的第二個陣列中,因此可以說搜索變得動態,現在所有作者的書都被顯示出來,當作者的姓氏從搜索中被抹去時,陣列的其余部分被載入,而不僅僅是搜索中輸入的內容。
在widgets的樹中,我們所做的一切和我的問題一樣,唯一不同的是,在_listItem中,我們只傳遞了index,而沒有其他內容,為了讓加載正確發生,我只需要在ListView.builder前面的主widget中添加加載,使用三元運算子:
body: (_routes == null) ? CircularProgressIndicator() : ValueListenableBuilder(
builder: (BuildContext context, value, Widget child) {
return ListView.builder(
itemCount: _routesToDisplay.length 1,
itemBuilder。(context, index) {
return index == 0 ?
? _searchBar()
: _listItem(index-1)。
},
);
P.S不需要ValueListenableBuilder,我開始在資料庫中保存選定的元素,只有這一行需要加載串列項。(_routes == null)? CircularProgressIndicator()。ListView.builder
另外,在顯示串列和搜索的部件中,不要用_routes,而是添加_routesToDisplay,結果是這樣的:
_listItem(index) {
return ListTile(
標題。Text(_routesToDisplay[index].route.mrTitle)。
領導。Text(
_routesToDisplay[index].route.mrNum,
風格。TextStyle(color: Colors.green, fontSize: 20) 。
),
后面的。IconButton(
icon: getIcon(index),
onPressed: () => onFavoritePress(index, _routes),
),
onTap: () {
Navigator.push(
背景關系。
MaterialPageRoute(
builder: (context) => StopsPage(
routeWithStops: _routes[index],
)));
},
);
}
_searchBar() {
print('test1')。
return Padding(
padding。const EdgeInsets.all(8) 。
孩子。TextField(
裝飾。InputDecoration(
hintText: 'Search'。
hoverColor: Colors.green,
邊界。概要輸入邊框(
borderRadius: borderRadius.round(10)。
),
prefixIcon: Icon(Icons.search),
),
onChanged: (text) {
text = text.toLowerCase();
setState(() {
print('object')。
_routesToDisplay = _routes.where((element) {
var routesTitle = element.route.mrTitle.toLowerCase()。
print(routeTitle)。
return routesTitle.contains(text);
}).toList()。
});
},
),
);
}
當然,你可以洗掉所有的印刷品,并按你的意愿使用它。這是個很簡單的任務,但我是個新手,并沒有像它需要的那樣謹慎。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/332972.html
標籤:
