“_InternalLinkedHashMap<String, dynamic>”不是“BanarModel”类型的子类型

时间:2021-06-04 01:40:14

标签: json list flutter

<块引用>

这里有什么问题。我想是因为我将对象存储在列表中,所以我如何解决它的错误是“_InternalLinkedHashMap”不是“BanarModel”类型的子类型。

class HomeModel 
{
  bool status;
  HomeDataModel data;  
  HomeModel.fromJson(Map<String, dynamic> json) {
  status = json['status'];
  data = HomeDataModel.fromJson(json['data']);
 }
}

class HomeDataModel 
{
  List<BanarModel> banners; 
  HomeDataModel.fromJson(Map<String, dynamic> json) {
  json['banners'].forEach((element) {
  banners.add(element);
  });   
 }
}   

class BanarModel 
{
  var id;
        String token;
        String image;
        BanarModel.fromJson(Map<String, dynamic> json) {
        id = json['id'];
        token = json['token'];
        image = json['image'];
        }
}   
HomeModel homeModel;
  void getHomeData(){
    emit(ShopLoadingHomeDataStat());
   DioHelper.getData(url: Home,token:token ,).then((value){
   homeModel=HomeModel.fromJson(value.data);
  print(homeModel.data.banners[0].image);
   emit(ShopSuccessHomeDataStat());

 }).catchError((onError){
   print(onError.toString());
   emit(ShopErorrHomeDataStat(error: onError));
 });

<块引用>

我打开应用时直接调用

BlocProvider(create: (Context)=>ShopCubit()..getHomeData()) 

1 个答案:

答案 0 :(得分:1)

像下面这样更新您的 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <head> <meta charset="UTF-8"> <title>Products</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> </head> <body> <p align="center"><h2>Products</h2></p> <br> <table class="table table-striped"> <tr><th>ID</th><th>Category</th><th>Brand</th><th>Model</th><th>Price, $</th><th>Quantity</th></tr> <tr th:each="product: ${products}"> <td th:text="${product.id}">ID</td> <td th:text="${product.category}">Category</td> <td th:text="${product.brand}">Brand</td> <td th:text="${product.model}">Model</td> <td th:text="${product.price}">Price, $</td> <td th:text="${product.quantity}">Quantity</td></tr> <td><a href="#" th:href="@{/delete(id=${product.id})}" class="btn btn-danger"> <i class="fa fa-trash-o fa-lg" ></i>Delete</a></td> </tr> </table> <center> <a href="http://localhost:8080/addProduct">Add product</a> <br> <a href="http://localhost:8080/">Refresh</a> </center> </body> </html>

HomeDataModel