我有一个Stateful Class,如果有Items,它会构建一个网格。 可以通过对外部服务器的HTTP调用来检索这些项目。
我正在使用PagewiseGridView(https://pub.dartlang.org/packages/flutter_pagewise)创建网格。
PagewiseGridView有一个名为totalCount的参数,它是一个INT,当我从Future获取总数时,它可以完美地工作,因为我已经在initstate()中设置了一个INT var来在返回Future之后更改值。
真正的问题是当我在外部服务器上进行搜索时。 对于搜索,我正在构建一个类:ShowSearch扩展了SearchDelegate,搜索委托具有名为buildResult的小部件,该小部件将显示我从外部调用中获得的结果。
我还使用PagewiseGridView构建结果布局。
代码如下:
@override
Widget buildResults(BuildContext context) {
// TODO: implement buildResults
return PagewiseGridView(
pageSize: 10,
totalCount: BackendService.getSearchTotals(query),
crossAxisCount: 2,
mainAxisSpacing: 10.0,
crossAxisSpacing: 5.0,
//childAspectRatio: 0.802,
padding: EdgeInsets.all(5.0),
itemBuilder: _itemBuilder,
pageFuture: (pageIndex){
return BackendService.getSearchItems(pageIndex, query);
},
);
}
“ totalCount:BackendService.getSearchTotals(query)”返回一个Future,因此它不起作用,因为它需要一个INT(totalCount:100,有效)。
我该如何解决?
全类(不同方法):
class ShowSearch extends SearchDelegate<BackendService> {
Color _mainColor = const Color(0xFFCA0813);
int _searchTotalCounter;
@override
void initState() {
//super.initState();
getSearchTotalCounter();
}
void getSearchTotalCounter() async {
_searchTotalCounter = await getSearchTotals(query);
//setState(() {});
}
Future<int> getSearchTotals(query) async {
var myRes = await http.get(Uri.encodeFull("https://www.mydomain.io/wp-json/test/v1/stats/search/?s=$query"), headers: {"Accept": "application/json"});
var myResBody = json.decode(myRes.body);
return myResBody["count_total"];
}
@override
List<Widget> buildActions(BuildContext context) {
// TODO: implement buildActions
return [IconButton(
icon: Icon(Icons.clear),
onPressed: (){
query = "";
}
)];
}
@override
Widget buildLeading(BuildContext context) {
// TODO: implement buildLeading
return IconButton(icon: Icon(Icons.arrow_back), onPressed: (){
close(context, null);
});
}
@override
Widget buildSuggestions(BuildContext context) {
// TODO: implement buildSuggestions
return Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(query)
)
)
],
);
}
@override
Widget buildResults(BuildContext context) {
// TODO: implement buildResults
return PagewiseGridView(
pageSize: 10,
totalCount: _searchTotalCounter,
//totalCount: 20 //IT WORKS!
crossAxisCount: 2,
mainAxisSpacing: 10.0,
crossAxisSpacing: 5.0,
//childAspectRatio: 0.802,
padding: EdgeInsets.all(5.0),
itemBuilder: _itemBuilder,
pageFuture: (pageIndex){
return BackendService.getSearchItems(pageIndex, query);
},
);
}
}
我评论了super.initState()和setState(),因为它们给出了一个错误(错误:方法'initState'未在'SearchDelegate'的超类中定义。)和(错误:方法'setState '未为“ ShowSearch”类定义。)
https://www.mydomain.io/wp-json/test/v1/stats/search/?s=$query
返回
{"status":"ok","total":10,"count_total":502,"pages":51,"query":"beans"}
答案 0 :(得分:1)
首先,您可以添加totalCount:count,并将count设置为0,然后调用此此处提到的getCount()方法,将发生什么情况,首先将调用BackendService.getSearchTotals(query),完成后将返回一个Future,然后您可以使用结果
使用setState更新计数并重建,
ps:您可以在getCount工作时显示循环加载程序。
Future getCount() async {
//here you can call the function and handle the output(return value) as result
BackendService.getSearchTotals(query).then((result) {
// print(result);
setState(() {
//handle your result here and update the count.
//update build here.
});
}).catchError(handleError);//you can call handleError method show an alert or to try again
}