首页 > 解决方案 > snapshot.data is NULL flutter

问题描述

I'm starter in flutter . I would like to fetch data from Complex json using API . Using postman my response body contain publication object and inside it i have i list of objects. . firstly i created Publication model and i try to use it for fetching data but snapshot.data is still 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 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) :

enter image description here

and i got this error when inspect(snapshot);

enter image description here

标签: flutterdart

解决方案


为了这条线工作

  snapshot.data.publication.length;

此行必须返回一个必须有发布且必须是列表的对象

   getproduct(widget.idproduct);

推荐阅读