来自Model类的 List不存储在控制器类的新列表中


**Product Modelclass which extracted from json file**

class Product {
int? _totalSize;
int? _typeId;
int? _offset;
late List<ProductModel> _products;
List<ProductModel> get products=> _products;
Product({required totalSize, required typeId, required offset, required products}){
this. _totalSize=totalSize;
this. _typeId=typeId;
this. _offset=offset;
this. _products=products;
}
Product.fromJson(Map<String, dynamic> json) {
_totalSize = json['total_size'];
_typeId = json['type_id'];
_offset = json['offset'];
if (json['productModel'] != null) {
_products= <ProductModel>[];
json['products'].forEach((v) {
_products.add(new ProductModel.fromJson(v));
});
}
}
}
class ProductModel {
int? id;
String? name;
String? description;
int? price;
int? stars;
String? img;
String? location;
String? createdAt;
String? updatedAt;
int? typeId;
ProductModel(
{this.id,
this.name,
this.description,
this.price,
this.stars,
this.img,
this.location,
this.createdAt,
this.updatedAt,
this.typeId});
ProductModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
description = json['description'];
price = json['price'];
stars = json['stars'];
img = json['img'];
location = json['location'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
typeId = json['type_id'];
}

}

这是我的Api_client类扩展Getx从服务器获取响应

import 'package:get/get.dart';
class Api_client extends GetConnect{
late String token;
late String AppbaseUrl;
late Map<String,String> _mainHeader;
Api_client({required this.AppbaseUrl}){
baseUrl=AppbaseUrl;
timeout=Duration(seconds: 30);
token="";
_mainHeader={
'Content-type':' application/json; charset-UTF-8',
'Authorization':' Bearer $token',
};
}
Future <Response> getData(String uri) async{
try{
Response response = await get(uri);
return response;
}catch(e){
return Response(statusCode: 1,statusText: e.toString());
}

}

}
**This is Popular_Product_List_Repo class extending Getx 
getservices to get response from Api_client** 
import 'package:get/get.dart';
import 'package:untitled/data/api/Api_client.dart';
class Popular_Product_List_Repo extends GetxService{
final Api_client apiClient;
Popular_Product_List_Repo({ required this.apiClient});
Future <Response> get_popular_product_list()async{
return await 
apiClient.getData("/api/v1/products/popular");
}

}

这是我的控制器类Popular_Product_Controller,它负责从Popular_Product_List_Repo获取响应并检索列表并将列表存储在我新创建的列表中,该列表是List_Popular_product_list=[];在我的UI上显示列表

import 'package:get/get.dart';
import 'package:untitled/data/Repository/Popular_Product_List_Repo.dart';
import 'package:untitled/data/models/Products_model.dart';
class Popular_Product_Controller extends GetxController{
final   Popular_Product_List_Repo popular_product_repo ;
List<dynamic>_Popular_product_list=[];
Popular_Product_Controller({required this.popular_product_repo});
Future getPopular_Product_list() async{
Response response=await popular_product_repo.get_popular_product_list();
if(response.statusCode==200){
print("got products");
_Popular_product_list=[];
_Popular_product_list.addAll(Product.fromJson(response.body).products);
print(_Popular_product_list);
update();
}else{
}

}


}

问题是我要确保响应从服务器和存储在我的列表中没有任何问题_Popular_product_list Popular_Product_Controller我写print语句中创建的,我这是有产品和_Popular_product_list本身来检查数据是否在_Popular_product_list当我运行它不显示任何陈述这意味着函数getPopular_Product_list()并不像我期望的工作是什么错,为什么列表

In order to verify if you are getting some data.. you can use the **Either** class in Dart which can imported from pub.dev. its make it easy to handle
for example, 
final Either<String, List<Product>> result =
await getProductList();

result.fold((exception) {
CustomToast.showErrorToast(Messages.UNABLE_TO_LOAD_PRODUCT_LIST);
}, (products) async {
//get products here and you can do what you want
}

相关内容

最新更新