Flutter 使用“异步”资源缓存HTTP请求`

Flutter 使用“异步”资源缓存HTTP请求`,flutter,Flutter,Flatter新手,我正在尝试使用async\u资源package缓存HTTP请求 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'; Futur

Flatter新手,我正在尝试使用
async\u资源
package缓存HTTP请求

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();
  }
} 
导入'dart:async';
导入“dart:convert”;
导入'package:async_resource/async_resource.dart';
导入'package:async_resource/file_resource.dart';
导入“package:path_provider/path_provider.dart”;
Future fetchPosts()异步{
最终路径=(等待getApplicationDocumentsDirectory()).path;
最终发布=HttpNetworkResource(
网址:'http://example.com/api/posts',
解析器:(contents)=>Posts.fromJson(json.decode(contents)),
缓存:FileResource(文件(“$path/posts”),
最大值:持续时间(秒数:2),
策略:CacheStrategy.cacheFirst,
);
等待posts.get().then((data)=>posts.fromJson(data.toJsonEncodable());
} 
班岗{
最终id;
最终标题;
最后副标题;
最后说明;
最终图像;
Post({this.id,this.title,this.subtitle,this.description,this.image});
静态fromJson(映射json){
回程站(
id:json['id'],
标题:json['title'],
副标题:json['subtitle'],
description:json['description'],
image:json['image'],
);
}
toJsonEncodable(){
Map m=新Map();
m['id']=id;
m['title']=标题;
m['subtitle']=副标题;
m['description']=描述;
m['image']=图像;
返回m;
}
}
班级职务{
最后员额名单;
Posts({this.Posts});
factory Posts.fromJson(列表parsedJson){
列表帖子=新列表();
posts=parsedJson.map((i)=>Post.fromJson(i)).toList();
返回新职位(职位:职位);
}
toJsonEncodable(){
返回posts.map((post){
return post.tojsonecodable();
}).toList();
}
} 
通常,它应该返回缓存列表,但不会返回


我得到了
未处理的异常:类型“List”不是类型“List”的子类型

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

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();
  }
}
导入'dart:convert';
导入'package:async_resource/async_resource.dart';
导入'package:async_resource/file_resource.dart';
导入“package:path_provider/path_provider.dart”;
Future fetchPosts()异步{
最终路径=(等待getApplicationDocumentsDirectory()).path;
最终发布=HttpNetworkResource(
网址:'http://example.com/api/posts',
解析器:(contents)=>json.decode(contents),
缓存:FileResource(文件(“$path/posts”),
最大值:持续时间(天数:30),
策略:CacheStrategy.cacheFirst,
);
final myData=wait posts.get();
return Posts.fromJson(myData);
}
班岗{
最终字符串id;
最终标题;
最后副标题;
最后说明;
最终字符串图像;
Post({this.id,this.title,this.subtitle,this.description,this.image});
factory Post.fromJSON(映射json){
回程站(
id:json['id'],
标题:json['title'],
副标题:json['subtitle'],
description:json['description'],
image:json['image'],
);
}
toJsonEncodable(){
Map m=新Map();
m['id']=id;
m['title']=标题;
m['subtitle']=副标题;
m['description']=描述;
m['image']=图像;
返回m;
}
}
班级职务{
最后员额名单;
Posts({this.Posts});
factory Posts.fromJson(列表parsedJson){
列表帖子=新列表();
posts=parsedJson.map((i)=>Post.fromJsons(i)).toList();
返回新职位(职位:职位);
}
toJsonEncodable(){
返回posts.map((post){
return post.tojsonecodable();
}).toList();
}
}

它返回的是实例而不是json。

我们可以知道这发生在哪一行吗?posts=parsedJson.map((i)=>Post.fromJson(i)).toList();`Post.fromJson(i)).toList()`将
列表
返回给
列表
类型的变量,因此出现错误。可以使用:
返回Posts.fromJson(json.decode(response.body))
with
response=wait http.get()
是的,它不会。它只返回该类的一个实例。