【问题标题】:length of server response in flutter http packageFlutter http包中服务器响应的长度
【发布时间】:2020-08-25 13:47:08
【问题描述】:

我正在使用 future builder 来构建列表视图。我收到来自服务器的响应。我得到了我需要的东西。我正在尝试解决一个错误。错误是当我使用http.get 方法从服务器获取数据时:String data=response.body; 我得到了我想要的正确响应,我将其解析为String catagoeryId=jsonDecode(data)["data"][i]["_id"];

现在我在解码这个 json 时遇到的问题是我使用 for 循环在我的构造函数中传递了这个字符串。但我必须给长度以停止循环。我使用的长度是:data.length。它解码我的 json 响应并在我的构造函数中传递。但是在 json 长度结束后它停止工作并崩溃。我检查了data.length 的长度,大约是 344。但我只有 3 个对象。这是我用于解析的代码:

Future<List<ProductCatagoery>> getCatagories()async{
http.Response response = await http.get("http://138.68.134.226:3020/api/category/");
 List<ProductCatagoery> products=[];
  String data=response.body;
  for (int i=0;i<=data.length;i++) {
String catagoeryId=jsonDecode(data)["data"][i]["_id"];
String catagoeryThumb=jsonDecode(data)["data"][i]["thumb"];
String catagoeryName=jsonDecode(data)["data"][i]["name"];
bool catagoeryActive=jsonDecode(data)["data"][i]["active"];


print('name is: $catagoeryId : $catagoeryThumb : $catagoeryName : $catagoeryActive');
  ProductCatagoery newUser= 
  ProductCatagoery(catagoeryId,catagoeryThumb,catagoeryName,catagoeryActive);
  products.add(newUser);
  print('added ${newUser.id}');
  print('length is${products.length}');
  print('last length data: ${data.length}');
 } 
   return products;
 }

模型类:

class ProductCatagoery {
final String id;
 final String thumb;
 final String name;
 final bool active;
  ProductCatagoery(this.id,this.thumb,this.name,this.active);
 }

回复是:

{"success":true,"data":[{"_id":"5f13cc94c63abc03522eff41","thumb":"category/fresh-meat.jpg","name":"Fresh Meat","active" :true},{"_id":"5f13cc73c63abc03522eff40","thumb":"category/fruits-vegetables.jpg","name":"水果和蔬菜","active":true},{"_id":" 5f13cca5c63abc03522eff42","thumb":"category/grocery.jpg","name":"Grocery","active":true}]}

注意:我只需要String data=response.body; 数据长度。我没有使用地图等。如果我在第 1 次、第 2 次或第 3 次迭代后返回产品列表,我还会在列表中显示产品。

【问题讨论】:

  • 你能分享一下json响应吗?
  • {"success":true,"data":[{"_id":"5f13cc94c63abc03522eff41","thumb":"category/fresh-meat.jpg","name":"鲜肉","active":true},{"_id":"5f13cc73c63abc03522eff40","thumb":"category/fruits-vegetables.jpg","name":"水果和蔬菜","active":true},{ "_id":"5f13cca5c63abc03522eff42","thumb":"category/grocery.jpg","name":"Grocery","active":true}]}
  • 附加信息应编辑到问题中。
  • 首先解码,然后得到内部“数据”列表的长度。想想你实际得到的长度。

标签: flutter jsondecoder flutter-http


【解决方案1】:

首先,解码收到的响应

final responseFormat = json.decode(response.body);

然后,你就可以用这个得到你想要循环的列表了

final data = responseFormat["data"];

最后可以得到列表的长度:data.length

完整代码

List<ProductCatagoery> products = [];
  final responseFormat = json.decode(response.body);
  final data = responseFormat["data"];
  for (int i = 0; i < data.length; i++) {
    String catagoeryId = data[i]["_id"];
    String catagoeryThumb = data[i]["thumb"];
    String catagoeryName = data[i]["name"];
    bool catagoeryActive = data[i]["active"];

    print(
        'name is: $catagoeryId : $catagoeryThumb : $catagoeryName : $catagoeryActive');
    ProductCatagoery newUser = ProductCatagoery(
        catagoeryId, catagoeryThumb, catagoeryName, catagoeryActive);
    products.add(newUser);
    print('added ${newUser.id}');
    print('length is${products.length}');
    print('last length data: ${data.length}');
  }

【讨论】:

  • 向此代码添加解释将提高答案的质量,并使其对未来的用户更有用。
猜你喜欢
  • 2017-01-26
  • 1970-01-01
  • 2023-03-05
  • 2021-04-14
  • 2021-02-07
  • 2017-02-12
  • 2012-05-12
  • 2013-02-02
  • 1970-01-01
相关资源
最近更新 更多