如何解决我正在使用 Getx 进行状态管理,当我向服务器添加数据时,它不会在列表视图中显示较新的数据,直到列表视图在 Obx 中时热重启
我正在开发一个应用。它与其余 API 一起工作以从服务器获取数据。但是当我尝试向服务器添加数据时,列表中没有出现较新的数据。这是我的视图和控制器类代码。
查看课程代码。它在无状态小部件中
Expanded(
child: Obx(() {
if (controller.isLoading.value) {
return Center(child: LoadingBar());
} else {
return controller.profilesList.length == 0
? Center(child: Text("No Service Found"))
: ListView.builder(
physics: ScrollPhysics(),shrinkWrap: true,itemCount: controller.profilesList.length,itemBuilder: (context,index) {
return Card(
elevation: 5,shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(5))),clipBehavior: Clip.antiAlias,child: Container(
height: 100,child: Row(
children: [
Padding(
padding: const EdgeInsets.all(2.0),child: Container(
width: 100,// height: 100,decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(5)),image: DecorationImage(
image: NetworkImage(
'http://192.168.43.113:4000/${controller.profilesList[index].shopImage}'),fit: BoxFit.cover))),),Flexible(
child: Padding(
padding: const EdgeInsets.all(2.0),child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,mainAxisAlignment:
MainAxisAlignment.spaceBetween,children: [
Text(
controller
.profilesList[index].shopName,style: TextStyle(
color: Colors.black,fontWeight: FontWeight.bold,fontSize: 15),Text(
controller
.profilesList[index].address,maxLines: 1,overflow: TextOverflow.ellipsis,style: TextStyle(
color: Colors.black54)),Text('9:AM-10:PM Mon-Sat',style: OrdersTextStyle
.servicesTextStyle()),Align(
alignment: Alignment.bottomLeft,child: Container(
decoration: BoxDecoration(
color: CustomColors.lightRed,// border: Border.all(width: 1),borderRadius: BorderRadius.all(
Radius.circular(5)),child: Padding(
padding:
const EdgeInsets.all(3.0),child: Text(
controller
.profilesList[index]
.providercategories
.providerCatName,style: TextStyle(
color: Colors.white,fontWeight:
FontWeight.normal),// height: 25,// width: 70,)
],)
],);
},);
}
}),
这是我的控制器类
class ProviderProfilesController extends GetxController {
var id = ''.obs;
var isLoading = true.obs;
var profilesList = <ProfileModel>[].obs;
void getProfilesData(String id) async {
isLoading(true);
try {
var list = await ApiServices.getProvidersprofileData(id);
if (list != null) {
profilesList.value = list;
}
} finally {
isLoading(false);
}
//profilesList.refresh();
}
@override
void onInit() {
super.onInit();
getProfilesData(id.value);
}
}
解决方法
我认为这里的问题是您使用 profileList.value 来更新列表,正确的方法是使用 List 的函数将项目添加到列表中,如下例所示:
final abc = [0,1,2].obs;
abc.add(12);
在您的情况下,一旦您想添加整个列表,您可以使用以下代码:
abc.addAll(['12','234','1465']);
如果这不起作用,请告诉我,因为 getx 上还有更新和刷新功能,有时可以解决我的问题。我会尽快回复。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。