0

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>'

7
  • May we know on which line is that happening? Commented Jul 22, 2019 at 11:51
  • posts = parsedJson.map((i) => Post.fromJson(i)).toList(); Commented Jul 22, 2019 at 12:34
  • ` Post.fromJson(i)).toList()` is returning a List<dynamic> to a variable of type List<Post> hence the error. Commented Jul 22, 2019 at 12:42
  • works fine with : return Posts.fromJson(json.decode(response.body)); with response = await http.get() Commented Jul 22, 2019 at 12:51
  • 1
    Yup it won't. It just returns an instance of that class. Commented Jul 25, 2019 at 4:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.