首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用`async_resource`缓存HTTP请求

用`async_resource`缓存HTTP请求
EN

Stack Overflow用户
提问于 2019-07-22 11:35:55
回答 1查看 871关注 0票数 0

我正在尝试用async_resource包缓存HTTP请求

代码语言:javascript
复制
import 'dart:async';
import 'dart:convert';
import 'package:async_resource/async_resource.dart';
import 'package:async_resource/file_resource.dart';
import 'package:path_provider/path_provider.dart';



Future <Posts> fetchPosts() async {
  final path = (await getApplicationDocumentsDirectory()).path;

  final posts = HttpNetworkResource<Posts>(
    url: 'http://example.com/api/posts',
    parser: (contents) => Posts.fromJson(json.decode(contents)),
    cache: FileResource(File('$path/posts')),
    maxAge: Duration(seconds: 2),
    strategy: CacheStrategy.cacheFirst,
  );
  await posts.get().then((data) =>  Posts.fromJson( data.toJsonEncodable()));

} 

class Post{
  final id;
  final title;
  final subtitle;
  final description;
  final image;

  Post({this.id, this.title, this.subtitle, this.description, this.image});

  static fromJson(Map<String, dynamic> json){
    return Post(
      id: json['id'],
      title: json['title'],
      subtitle: json['subtitle'],
      description: json['description'],
      image: json['image'],
    );
  }
  toJsonEncodable(){
    Map<String, dynamic> m = new Map();
    m['id'] =id;
    m['title'] =title;
    m['subtitle'] =subtitle;
    m['description'] =description;
    m['image'] =image;
    return m;
  }


}


class Posts{
  final List<Post> posts;

  Posts({this.posts});

  factory Posts.fromJson(List<dynamic> parsedJson){
    List<Post> posts = new List<Post>();
    posts = parsedJson.map((i) => Post.fromJson(i)).toList();

    return new Posts(posts: posts);
  }
  toJsonEncodable(){
    return posts.map((post){
      return post.toJsonEncodable();
    }).toList();
  }
} 

通常,它应该返回缓存的列表,但不返回。

我要去找Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Post>'

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-25 08:07:26

为了回答这个问题,这是工作版本。

代码语言:javascript
复制
import 'dart:convert';
import 'package:async_resource/async_resource.dart';
import 'package:async_resource/file_resource.dart';
import 'package:path_provider/path_provider.dart';



 Future <Posts> fetchPosts() async {
  final path = (await getApplicationDocumentsDirectory()).path;

  final posts = HttpNetworkResource(
    url: 'http://example.com/api/posts',
    parser: (contents) => json.decode(contents),
    cache: FileResource(File('$path/posts')),
    maxAge: Duration(days: 30),
    strategy: CacheStrategy.cacheFirst,
  );
  final myData = await posts.get();
  return Posts.fromJson(myData);

}

class Post{
  final String id;
  final title;
  final subtitle;
  final description;
  final String image;

  Post({this.id, this.title, this.subtitle, this.description, this.image});

  factory Post.fromJsons(Map<String,dynamic> json){
    return Post(
      id: json['id'],
      title: json['title'],
      subtitle: json['subtitle'],
      description: json['description'],
      image: json['image'],
    );
  }
  toJsonEncodable(){
    Map<String, dynamic> m = new Map();
    m['id'] =id;
    m['title'] =title;
    m['subtitle'] =subtitle;
    m['description'] =description;
    m['image'] =image;
    return m;
  }
}
class Posts{
  final List<Post> posts;

  Posts({this.posts});

  factory Posts.fromJson(List<dynamic> parsedJson){
    List<Post> posts = new List<Post>();
    posts = parsedJson.map((i) => Post.fromJsons(i)).toList();
    return new Posts(posts: posts);
  }
  toJsonEncodable(){
    return posts.map((post){
      return post.toJsonEncodable();
    }).toList();
  }
}

它是返回实例而不是json。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57145225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档