数据未显示在列表视图抖动中

时间:2020-08-17 07:24:32

标签: flutter dart

我试图显示数据到列表视图,但它什么也没显示。下面是json,我做错了什么,我很累在api调用中打印数据,但是我可以打印数据,但不能在列表视图中打印, 我很难理解自己在做什么或做错什么。请为此提供帮助

if (response.statusCode == 200) {
      try {
        if (status == true) {

          var company_list = value['doc'];
          for (int i = 0; i < company_list.length; i++) {
            print(company_list);
            var mobile_list = company_list["mobile"];
            var email_list = company_list["email"];
            company_model.add(CompanyModel.fromJson(data,mobile_list,email_list));
          }
          setState(() {
            print("UI Updated");
          });
        } else {
          final snackBar = SnackBar(content: Text(message));
          _scaffoldKey.currentState.showSnackBar(snackBar);
        }
      } catch (e) {
        e.toString();
      }
    } else {
      final snackBar = SnackBar(content: Text(message));
      _scaffoldKey.currentState.showSnackBar(snackBar);
    }

Json

{
    "status":true,
    "doc":{
        "mobile":[
            "9961256754",
            "8974525672"
        ],
        "email":[

        ],
        "_id":"5f3a0dfe88b9d50453e92133",
        "name":"MRC Labour",
        "terms":{
            "english":"We Shall not be liable for any disputes arising between the users and the 
         labourers",
            "malayalam":"We Shall not be liable for any disputes arising between the users and the 
         labourers"
        }
    }
}

模型

class CompanyModel {
  String id = "";
  String mobile = "";
  String email = "";
  String name = "";
  String english = "";
  String malayalam = "";

  CompanyModel({this.id, this.mobile, this.email, this.name, this.english,this.malayalam});

  CompanyModel.fromJson(json,mobile_list,email_list)
      : id = json['_id'].toString(),
        mobile = mobile_list,
        email = email_list,
        name = json['name'].toString(),
        english = json['terms']['english'].toString(),
        malayalam = json['terms']['malayalam'].toString();
}

Data i am geting,

当调试器在此行命中时,它会中断而没有任何错误company_model.add(CompanyModel.fromJson(data,mobile_list,email_list));

2 个答案:

答案 0 :(得分:0)

您好,您的模型应该是这样

class CompanyModel {
    Doc doc;
    bool status;

    CompanyModel({this.doc, this.status});

    factory CompanyModel.fromJson(Map<String, dynamic> json) {
        return CompanyModel(
            doc: json['doc'] != null ? Doc.fromJson(json['doc']) : null, 
            status: json['status'], 
        );
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['status'] = this.status;
        if (this.doc != null) {
            data['doc'] = this.doc.toJson();
        }
        return data;
    }
}

class Doc {
    String id;
    List<String> email;
    List<String> mobile;
    String name;
    Terms terms;

    Doc({this.id, this.email, this.mobile, this.name, this.terms});

    factory Doc.fromJson(Map<String, dynamic> json) {
        return Doc(
            id: json['_id'], 
            email: json['email'] != null ? new List<String>.from(json['email']) : null, 
            mobile: json['mobile'] != null ? new List<String>.from(json['mobile']) : null, 
            name: json['name'], 
            terms: json['terms'] != null ? Terms.fromJson(json['terms']) : null, 
        );
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['_id'] = this.id;
        data['name'] = this.name;
        if (this.email != null) {
            data['email'] = this.email;
        }
        if (this.mobile != null) {
            data['mobile'] = this.mobile;
        }
        if (this.terms != null) {
            data['terms'] = this.terms.toJson();
        }
        return data;
    }
}

class Terms {
    String english;
    String malayalam;

    Terms({this.english, this.malayalam});

    factory Terms.fromJson(Map<String, dynamic> json) {
        return Terms(
            english: json['english'], 
            malayalam: json['malayalam'], 
        );
    }

    Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['english'] = this.english;
        data['malayalam'] = this.malayalam;
        return data;
    }
}

使用喜欢方式

CompanyModel model = CompanyModel.fromJson(value);

答案 1 :(得分:0)

根据发问者的要求,张贴我的旧代码以供参考。

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:flutter/foundation.dart';
import 'package:igloled_app/components/urls.dart';
import 'package:logger/logger.dart';


class Category with ChangeNotifier{

  var logger = Logger(
    printer: PrettyPrinter(),
  );

  List<SetCategory> allCategoryList = [];

  int get getlength{
      return allCategoryList.length;
  }

  bool get getAllCatisempty
  {
    return allCategoryList.isEmpty;
  }

  int get initialCatId{
    if(allCategoryList.isNotEmpty)
    {
      return allCategoryList[0].catId;
    }
    else
      {
        return 25504210;
      }

  }


Future<List<SetCategory>>  getAllCategories() async {


    try
    {
      Response allCategory = await FetchData().fetchCategoryData();

//      print('getAllCategories status ${allCategory.statusCode}');


      if (allCategory.statusCode == 200) {
        var categoryData = allCategory.body;
        int totalcount = jsonDecode(categoryData)['count'];

        if (allCategoryList.length != totalcount) {
          allCategoryList.clear();

          for (int i = 0; i < totalcount; i++) {
//            print(jsonDecode(categoryData)['items'][i]['id']);
//            print(jsonDecode(categoryData)['items'][i]['name']);

            allCategoryList.add(SetCategory(
              catId: jsonDecode(categoryData)['items'][i]['id'],
              catName: jsonDecode(categoryData)['items'][i]['name'],
              catThumbnail: jsonDecode(categoryData)['items'][i]['thumbnailUrl'],
            ));

          }

          notifyListeners();

          return allCategoryList;
        }
      }
      else
        {
          notifyListeners();


          return allCategoryList;

        }
//      print('allcategorylist length ${allCategoryList.length}');


    }
    catch(e){


      logger.e(e);


    }

    notifyListeners();
    return allCategoryList;


}



}

class SetCategory {

  int catId;
  String catName;
  String catThumbnail;

  SetCategory(
      { this.catId, this.catName, this.catThumbnail});

}