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
Try to replace
News.fromJson(responseJson);
withNews().fromJson(responseJson);
You should instantiate New class then call instance method
fromJson
this method is not static so can’t be accessed directly.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:I am assuming your News data model has title and content fields, feel free to change it according to your exact data model.