Flutter 将SliverList与FutureBuilder项一起使用添加但不更新列表分页,颤振
我得到的不是错误,但问题是当我获取下一个10条记录时,记录出现并打印在Flutter 将SliverList与FutureBuilder项一起使用添加但不更新列表分页,颤振,flutter,future,flutter-sliver,stream-builder,flutter-futurebuilder,Flutter,Future,Flutter Sliver,Stream Builder,Flutter Futurebuilder,我得到的不是错误,但问题是当我获取下一个10条记录时,记录出现并打印在logcat中,但在SliverList记录未添加到列表中,我不知道确切的问题是什么,数据出现并更新列表,然后突然恢复到列表长度的先前状态,意思是当我尝试fetchmore方法时,列表长度更新为10到20,突然又回到了10,所以请帮我解决这个问题 @override void initState() { super.initState(); _controller = ScrollController(); _contro
logcat
中,但在SliverList
记录未添加到列表中,我不知道确切的问题是什么,数据出现并更新列表,然后突然恢复到列表长度的先前状态,意思是当我尝试fetchmore
方法时,列表长度更新为10到20,突然又回到了10,所以请帮我解决这个问题
@override
void initState() {
super.initState();
_controller = ScrollController();
_controller.addListener(_scrollListener);
loadMyProduct();
}
_scrollListener() {
if (_controller.offset >= _controller.position.maxScrollExtent &&
!_controller.position.outOfRange) {
fetchMoreProduct(listProduct.length+1);
}
if (_controller.offset <= _controller.position.minScrollExtent &&
!_controller.position.outOfRange) {
setState(() {
print("at top");
});
}
}
bool isMoreRunning=false;
fetchMoreProduct(int newLength)async{
//if(isMoreRunning==false && totalProducts>newLength){
print("new length "+newLength.toString());
isMoreRunning=true;
visibleMore=true;
print(totalProducts);
var url="http://ochapchap.com/api/productList";
SharedPreferences prefs = await SharedPreferences.getInstance();
String userid=prefs.getString('userid');
var data = {
"startLimit":newLength.toString(),
"language":"en",
"userId":userid};
var response = await http.post(url, body: json.encode(data));
if(response.statusCode==200) {
FeedCheckProduct checkLogin =FeedCheckProduct.fromJson(json.decode(response.body));
if(checkLogin.success==1){
List<FeedProduct> newProduct=(jsonDecode(response.body)['result'] as List).map((i) => FeedProduct.fromJson(i)).toList();
newProduct.forEach((element) async{
print(element.name+" "+element.price);
listProduct.add(element);
});
print("NEW PRODUCT LENGTH "+listProduct.length.toString());
setState(() {
listProduct.length;
});
visibleMore=false;
}
/*}else{
setState(() {
isMoreRunning=false;
isProductLoad=false;
visibleMore=false;
});
Fluttertoast.showToast(
msg: "Try after some time!.",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0
);
}*/
}
}
Future<List<FeedProduct>> loadProduct()async{
Future.delayed(const Duration(seconds: 3));
setState(() {
isProductLoad=true;
});
Map<String, String> requestHeaders = {
'Content-type': 'application/json',
'Accept': 'application/json',
};
var url="http://ochapchap.com/api/productList";
SharedPreferences prefs = await SharedPreferences.getInstance();
String userid=prefs.getString('userid');
String language=prefs.getString('language');
if(language==null){
language="en";
}
var data = {
"startLimit":"0",
"language": language,
"userId":userid};
var response = await http.post(url, body: json.encode(data),headers: requestHeaders);
if(response.statusCode==200) {
FeedCheckProduct checkLogin =FeedCheckProduct.fromJson(json.decode(response.body));
setState(() {
totalProducts=checkLogin.totalCount;
});
if(checkLogin.success==1){
Map<String, dynamic> mainData=json.decode(response.body);
listProduct=(jsonDecode(response.body)['result'] as List).map((i) => FeedProduct.fromJson(i)).toList();
print("rrrrrrrrrrrrrrrrrrrr "+listProduct.length.toString());
return listProduct;
}
}else{
setState(() {
isProductLoad=false;
});
}
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ShowProduct(),
);
}
Widget ShowProduct(){
return Column(
children: <Widget>[
Expanded(
child: FutureBuilder(
future: loadProduct(),
builder: (BuildContext context,AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return CustomScrollView(
controller: _controller,
slivers: <Widget>[
SliverAppBar(
title: Text("Home"),
floating: true,
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index){
return ProductList(
product:listProduct[index], index: index,);
},
childCount: listProduct.length,
),
),
],
);
Visibility(
visible: visibleMore,
child: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 30.0),
child: new CircularProgressIndicator(),
),
)
],
);
@覆盖
void initState(){
super.initState();
_控制器=滚动控制器();
_controller.addListener(_scrollListener);
loadMyProduct();
}
_scrollListener(){
如果(_controller.offset>=_controller.position.maxScrollExtent&&
!\u控制器。位置。超出范围){
fetchMoreProduct(listProduct.length+1);
}
if(_controller.offset newLength){
打印(“新长度”+newLength.toString());
isMoreRunning=true;
visibleMore=真;
印刷品(产品);
变量url=”http://ochapchap.com/api/productList";
SharedReferences prefs=等待SharedReferences.getInstance();
字符串userid=prefs.getString('userid');
风险值数据={
“startLimit”:newLength.toString(),
“语言”:“en”,
“userId”:userId};
var response=wait http.post(url,body:json.encode(data));
if(response.statusCode==200){
FeedCheckProduct checkLogin=FeedCheckProduct.fromJson(json.decode(response.body));
if(checkLogin.success==1){
将newProduct=(jsonDecode(response.body)['result']列为List.map((i)=>FeedProduct.fromJson(i)).toList();
newProduct.forEach((元素)异步{
打印(element.name+“”+element.price);
添加(元素);
});
打印(“新产品长度”+listProduct.LENGTH.toString());
设置状态(){
listProduct.length;
});
visibleMore=假;
}
/*}否则{
设置状态(){
isMoreRunning=false;
isProductLoad=false;
visibleMore=假;
});
烤面包片(
味精:“过一段时间再试试!”,
烤面包片长度:烤面包片长度,
地心引力:ToastGravity.CENTER,
TimeInSecureForiosWeb:1,
背景颜色:Colors.red,
textColor:Colors.white,
字体大小:16.0
);
}*/
}
}
Future loadProduct()异步{
延迟(常数持续时间(秒:3));
设置状态(){
isProductLoad=true;
});
映射请求头={
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”,
};
变量url=”http://ochapchap.com/api/productList";
SharedReferences prefs=等待SharedReferences.getInstance();
字符串userid=prefs.getString('userid');
String language=prefs.getString('language');
if(语言==null){
语言=“en”;
}
风险值数据={
“startLimit”:“0”,
“语言”:语言,
“userId”:userId};
var response=wait http.post(url,body:json.encode(数据),headers:requestHeaders);
if(response.statusCode==200){
FeedCheckProduct checkLogin=FeedCheckProduct.fromJson(json.decode(response.body));
设置状态(){
totalProducts=checkLogin.totalCount;
});
if(checkLogin.success==1){
Map mainData=json.decode(response.body);
listProduct=(jsonDecode(response.body)['result']作为List.map((i)=>FeedProduct.fromJson(i)).toList();
打印(“rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr;
退货清单产品;
}
}否则{
设置状态(){
isProductLoad=false;
});
}
}
@凌驾
无效处置(){
super.dispose();
_controller.dispose();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:ShowProduct(),
);
}
Widget ShowProduct(){
返回列(
儿童:[
扩大(
孩子:未来建设者(
future:loadProduct(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData){
返回自定义滚动视图(
控制器:_控制器,
条子:[
滑杆(
标题:文本(“主页”),
浮动:是的,
),
银表(
代表:SliverChildBuilderDelegate(
(BuildContext上下文,int索引){
退货产品清单(
产品:列表产品[索引],索引:索引,);
},
childCount:listProduct.length,
),
),
],
);
可见度(
可见:visibleMore,
孩子:填充(
填充:LTRB(0.0,8.0,0.0,30.0)中的常量边集,
子项:新的CircularProgressIndicator(),
),
)
],
);
}有一个更好的答案,或者不要使用FutureBuilder,因为它表示存储库接收到的数据。过程应该是显示初始数据->调用下一个N数据->将N条记录添加到同一列表->父窗口小部件应通知列表更新(即通过setState)