简体   繁体   English

snapshot.data 是 NULL flutter

[英]snapshot.data is NULL flutter

I'm starter in flutter.我是 flutter 的初学者。 I would like to fetch data from Complex json using API.我想使用 API 从 Complex json 中获取数据。 Using postman my response body contain publication object and inside it i have i list of objects.使用 postman 我的响应正文包含出版物 object ,其中我有我的对象列表。 . . firstly i created Publication model and i try to use it for fetching data but snapshot.data is still NULL.首先,我创建了出版物 model 并尝试使用它来获取数据,但 snapshot.data 仍然是 NULL。 any help please??请问有什么帮助吗??

 @override
      void initState() {
        getproduct(widget.idproduct);
        super.initState();
      }
    
      Future<Publication> getproduct(int id) async {
        var response = await Network().getData('/publication/show/$id');
        return Publication.fromJson(json.decode(response.body['publication']));
       
      }

child: SingleChildScrollView(
                child: FutureBuilder<Publication>(
                  future: getproduct(widget.idproduct),
                  builder: (BuildContext context, AsyncSnapshot snapshot) {
                    inspect(snapshot.data);

                    if (snapshot.hasData) {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          for (int i = 0;
                              i < snapshot.data.publication.length;
                              i++)
                            CommentsList(
                              comment: snapshot.data.publication[i].comment,
                            
                            )
                        ],
                      );
                    }

and this my Publication Class:这是我的出版物 Class:

class Publication {
  int id;
  int userId;
  String name;
  String description;
  String category;
  int quantity;
  String size;
  String brand;
  String forWho;
  String color;
  String delivery;
  String price;
  int progression;
  String discount;
  int visibility;
  String status;
  int softdelete;
  String createdAt;
  String updatedAt;
  String picture1;
  String picture2;
  String picture3;
  Null picture4;
  String picture5;
  List<Comment> comment;
  String ownerpicture;

  Publication(
      {this.id,
      this.userId,
      this.name,
      this.description,
      this.category,
      this.quantity,
      this.size,
      this.brand,
      this.forWho,
      this.color,
      this.delivery,
      this.price,
      this.progression,
      this.discount,
      this.visibility,
      this.status,
      this.softdelete,
      this.createdAt,
      this.updatedAt,
      this.picture1,
      this.picture2,
      this.picture3,
      this.picture4,
      this.picture5,
      this.comment,
      this.ownerpicture});

  Publication.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    name = json['name'];
    description = json['description'];
    category = json['category'];
    quantity = json['quantity'];
    size = json['size'];
    brand = json['brand'];
    forWho = json['for_who'];
    color = json['color'];
    delivery = json['delivery'];
    price = json['price'];
    progression = json['progression'];
    discount = json['discount'];
    visibility = json['visibility'];
    status = json['status'];
    softdelete = json['softdelete'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    picture1 = json['picture1'];
    picture2 = json['picture2'];
    picture3 = json['picture3'];
    picture4 = json['picture4'];
    picture5 = json['picture5'];
    if (json['comment'] != null) {
      comment = new List<Comment>();
      json['comment'].forEach((v) {
        comment.add(new Comment.fromJson(v));
      });
    }
    ownerpicture = json['ownerpicture'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['name'] = this.name;
    data['description'] = this.description;
    data['category'] = this.category;
    data['quantity'] = this.quantity;
    data['size'] = this.size;
    data['brand'] = this.brand;
    data['for_who'] = this.forWho;
    data['color'] = this.color;
    data['delivery'] = this.delivery;
    data['price'] = this.price;
    data['progression'] = this.progression;
    data['discount'] = this.discount;
    data['visibility'] = this.visibility;
    data['status'] = this.status;
    data['softdelete'] = this.softdelete;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    data['picture1'] = this.picture1;
    data['picture2'] = this.picture2;
    data['picture3'] = this.picture3;
    data['picture4'] = this.picture4;
    data['picture5'] = this.picture5;
    if (this.comment != null) {
      data['comment'] = this.comment.map((v) => v.toJson()).toList();
    }
    data['ownerpicture'] = this.ownerpicture;
    return data;
  }
}

I got the following error when i print (snapshot):打印(快照)时出现以下错误:

在此处输入图像描述

and i got this error when inspect(snapshot);检查时出现此错误(快照);

在此处输入图像描述

It has error, use snapshot.hasError to handle and show the errorMessage,它有错误,使用snapshot.hasError处理并显示 errorMessage,

and error is in your model/Pojo class, something declared as int but it is String.并且错误在您的模型/Pojo class 中,声明为 int 但它是字符串。 Please check & compare your response and pojo/model.请检查并比较您的回复和 pojo/model。

Check at 60:58 of the PetDetail.dart file在 PetDetail.dart 文件的 60:58 检查

This might help you https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html#widgets.FutureBuilder.1这可能会帮助你https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html#widgets.FutureBuilder.1

in order this line to work为了这条线工作

  snapshot.data.publication.length;

this line must return an object which must have publication and that must be a list此行必须返回一个 object,它必须有发布并且必须是一个列表

   getproduct(widget.idproduct);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM