Flak
Flak

Reputation: 2670

Cache HTTP Request with `async_resource`

New to Flutter and I am trying to cache HTTP Request with async_resource package

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

Normally it should return cached list but it doesn't.

I am getting Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Post>'

Upvotes: 0

Views: 970

Answers (1)

Flak
Flak

Reputation: 2670

For sake of answered question, here is working version

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

It was returning Instance instead of json.

Upvotes: 1

Related Questions