snapshot.data 为 NULL 颤振

时间:2021-03-31 10:31:54

标签: flutter dart

我是 flutter 的初学者。我想使用 API 从 Complex json 获取数据。使用邮递员,我的响应正文包含发布对象,其中包含对象列表。 .首先,我创建了 Publication 模型,并尝试使用它来获取数据,但 snapshot.data 仍然为 NULL 。有什么帮助吗??

 @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,
                            
                            )
                        ],
                      );
                    }

这是我的出版课:

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;
  }
}

打印(快照)时出现以下错误:

enter image description here

我在检查(快照)时遇到了这个错误;

enter image description here

2 个答案:

答案 0 :(得分:0)

出现错误,使用snapshot.hasError处理并显示errorMessage,

并且错误在您的模型/Pojo 类中,声明为 int 但它是 String。请检查并比较您的回复和 pojo/model。

在 PetDetail.dart 文件的 60:58 处检查

这可能对你有帮助 https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html#widgets.FutureBuilder.1

答案 1 :(得分:0)

为了这条线工作

  snapshot.data.publication.length;

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

   getproduct(widget.idproduct);