解析可通行的json扑向podo

发布于 2025-01-28 01:46:40 字数 1771 浏览 2 评论 0原文

我是新手的扑朔迷离,并且在HTTP响应中解析JSON问题。

我正在使用Airtable后端来存储有关帖子的信息。这些总是包含图像,有时包含附件-PDF。

我构建了Podo,这样:

class Post {

String recordid;
String timecreated;
String title;
String content;
String imgurl;
List<Pdf>? pdf;

  Post({
    required this.recordid,
    required this.timecreated,
    required this.title,
    required this.content,
    required this.imgurl,
    required this.pdf
  });

  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(
        // fields: Fields.fromJson(json['fields']),
      recordid: json['id'] as String,
      timecreated: json['createdTime'] as String,
        title: json['fields']['field1'] as String,
        content: json['fields']['field2'] as String,
        imgurl: json['fields']['IMG'][0]['url'] as String,
        pdf: json['fields']['PDF'] == null ? null : List<Map<String, dynamic>>.from(json['fields']['PDF']).map((dynamic value) => Pdf.fromJson(value)).toList()
    );
  }

Map<String, dynamic> toJson() => {
  "recordid": recordid,
  "timecreated": timecreated,
  "title": title,
  "content": content,
  "imgurl": imgurl,
  "pdf": pdf
  // "fields": List<dynamic>.from(fields.map((x) => x.toJson())),
};
}

class Pdf {

  Pdf({
    required this.url,
    required this.filename
  });

  Pdf.fromJson(Map<String, dynamic> json) :
        url = json['url'],
        filename = json['filename'];

  final String? url;
  final String? filename;
}

我没有遇到任何错误,但是当我尝试在UI中使用PDF URL时。在文字中:

ListTile(title: Text(post.pdf.url)),

我遇到了错误:

属性“ URL”无法无条件访问,因为 接收器可以是“ null”。

我的目标是在页面上创建一个按钮,当存在PDF URL时,这是可单击的。当存在时,按钮将导航到使用PDF URL获取和显示PDF的PDF视图。

有什么想法吗?

I'm new in flutter and I have issue with parsing JSON on HTTP response.

I'm using Airtable backend, to store information about posts. These always contain images, and sometimes attachments - PDFs.

I built PODO, like this:

class Post {

String recordid;
String timecreated;
String title;
String content;
String imgurl;
List<Pdf>? pdf;

  Post({
    required this.recordid,
    required this.timecreated,
    required this.title,
    required this.content,
    required this.imgurl,
    required this.pdf
  });

  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(
        // fields: Fields.fromJson(json['fields']),
      recordid: json['id'] as String,
      timecreated: json['createdTime'] as String,
        title: json['fields']['field1'] as String,
        content: json['fields']['field2'] as String,
        imgurl: json['fields']['IMG'][0]['url'] as String,
        pdf: json['fields']['PDF'] == null ? null : List<Map<String, dynamic>>.from(json['fields']['PDF']).map((dynamic value) => Pdf.fromJson(value)).toList()
    );
  }

Map<String, dynamic> toJson() => {
  "recordid": recordid,
  "timecreated": timecreated,
  "title": title,
  "content": content,
  "imgurl": imgurl,
  "pdf": pdf
  // "fields": List<dynamic>.from(fields.map((x) => x.toJson())),
};
}

class Pdf {

  Pdf({
    required this.url,
    required this.filename
  });

  Pdf.fromJson(Map<String, dynamic> json) :
        url = json['url'],
        filename = json['filename'];

  final String? url;
  final String? filename;
}

I'm not getting any errors, but when I'm trying to use PDF URL in UI, eg. in Text:

ListTile(title: Text(post.pdf.url)),

I'm getting error:

The property 'url' can't be unconditionally accessed because the
receiver can be 'null'.

I'm aiming to create a button on a page, that is clickable when PDF URL exists. When it exists, button navigates to PDF view that use PDF URL to get and display PDF.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2025-02-04 01:46:40

pdf属性是无效的,因此无法无条件地访问它。这是假设您以某种方式没有pdf的列表,否则您需要索引列表,而您的代码不完整。您可以尝试做这样的事情:

  if (post.pdf != null) {
      //wrap it with a button or whatever
      return ListTile(title: Text(post.pdf!.url));
  }
  else {
      return Text('no pdf');
  }

The pdf attribute is nullable, hence it cannot be accessed unconditionally. This is assuming you somehow have the pdf not as a list, otherwise you would need to index your list, your code is incomplete. You could try to do something like this:

  if (post.pdf != null) {
      //wrap it with a button or whatever
      return ListTile(title: Text(post.pdf!.url));
  }
  else {
      return Text('no pdf');
  }
撑一把青伞 2025-02-04 01:46:40

好吧,STH似乎有效,但仍然无法解析PDF URL。

当post.pdf!= null时,我会收到文本“ sth wash wane”,并且它可以使用,但是当我更改以使用post.pdf!。

尝试将名称纠正为现有getter的名称或定义
名为“ URL”的Getter或字段。
孩子:文字(post.pdf!.url));

这是一件代码:

LayoutBuilder(builder: (context, constraints) {
                if(post.pdf != null){
                  return ElevatedButton(onPressed: () => Navigator.of(context).push(
                      MaterialPageRoute(builder: (context) => PDFview(pdf: [])
                      )),
                      child:Text(post.pdf!.url));
                }else{
                  return ElevatedButton(onPressed: null,
                      child:Text("no PDF"));
                }
              }
              )

Well, sth seems to work, but still cannot parse pdf URL.

I'm getting Text "sth is there" when post.pdf != null and it works, but when I'm changing to get value from model using post.pdf!.url I'm getting same error:

Try correcting the name to the name of an existing getter, or defining
a getter or field named 'url'.
child:Text(post.pdf!.url));

Here's piece of code:

LayoutBuilder(builder: (context, constraints) {
                if(post.pdf != null){
                  return ElevatedButton(onPressed: () => Navigator.of(context).push(
                      MaterialPageRoute(builder: (context) => PDFview(pdf: [])
                      )),
                      child:Text(post.pdf!.url));
                }else{
                  return ElevatedButton(onPressed: null,
                      child:Text("no PDF"));
                }
              }
              )
絕版丫頭 2025-02-04 01:46:40

Podo的修复程序对我有用。

这是解决我问题的解决方案:)

pdf: json['fields']['PDF'] == null ? null : json['fields']['PDF'][0]['url'] as String,

A fix in PODO worked for me.

Here's solution for my problem :)

pdf: json['fields']['PDF'] == null ? null : json['fields']['PDF'][0]['url'] as String,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文