skip to Main Content

I ran into a problem with my Flutter app. The error I get in news_service.dart is "The sample member ‘fromJson’ cannot be accessed using static access." error. Anyone know what this is about? I really don’t understand… Thanks in advance!!

if (response.body.isNotEmpty) {
  final responseJson = json.decode(response.body);
  News news = News.fromJson(responseJson);
  return news.articles;
}
return null;

}
}

3

Answers


  1. Try to replace News.fromJson(responseJson); with News().fromJson(responseJson);

    Login or Signup to reply.
  2. You should instantiate New class then call instance method fromJson this method is not static so can’t be accessed directly.

    final news = News();
    //now call news.fromJson(responseJson)
    
    Login or Signup to reply.
  3. The fromJson function needs to either be static, or a (factory) constructor. It is hard to give an exact working answer because you have not provided additional information that was asked from you, but the following sample fromJson implementation should work with little modification from your part:

    News.fromJson(Map<String, dynamic> json)
          : title = json['title'],
            content = json['content'];
    

    I am assuming your News data model has title and content fields, feel free to change it according to your exact data model.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search