'Error: Expected a value of type '(String, dynamic) => void', but got one of type '(dynamic) => Null'
My first project and newbie to flutter, unable to solve this after 2 weeks of trying.
I am trying to get request via API using GetX package and i got the error mentioned above.
Trying to parse JsonMap into a List and it's giving me the error mentioned.
I have tested and the StatusCode is 200, I am assuming its just not parsing correctly.
Thanks in advance!
Peace JY
Error message: -
Error: Expected a value of type '(String, dynamic) => void', but got one of type '(dynamic) => Null'
Controller:-
class BlogController extends GetxController {
final BlogRepo blogRepo;
BlogController({required this.blogRepo});
List<dynamic> _blogList = [];
List<dynamic> get blogList => _blogList;
Future<void> getBlogList() async {
Response response = await blogRepo.getBlogList();
if (response.statusCode == 200) {
print("Got Data"); //this gets printed
_blogList = [];
_blogList.addAll(Blog.fromJson(response.body).data);
print(_blogList); // not printing _blogList
update();
} else {}
}
}
Model:-
class Blog {
late List<Data> _data;
List<Data> get data => _data;
Blog({required data}) {
this._data = data;
}
Blog.fromJson(Map<String, dynamic> json) {
if (json['data'] != null) {
_data = <Data>[];
json['data'].forEach(
(v) {
_data.add(Data.fromJson(v));
},
);
}
}
}
class Data {
int? id;
Attributes? attributes;
Data({this.id, this.attributes});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
attributes = json['attributes'] != null
? Attributes.fromJson(json['attributes'])
: null;
}
}
class Attributes {
String? title;
String? createdAt;
String? updatedAt;
String? publishedAt;
String? text;
String? link;
Image? image;
Attributes(
{this.title,
this.createdAt,
this.updatedAt,
this.publishedAt,
this.text,
this.link,
this.image});
Attributes.fromJson(Map<String, dynamic> json) {
title = json['title'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
publishedAt = json['publishedAt'];
text = json['text'];
link = json['link'];
image = json['image'] != null ? Image.fromJson(json['image']) : null;
}
}
class Image {
Data? data;
Image({this.data});
Image.fromJson(Map<String, dynamic> json) {
data = json['data'] != null ? Data.fromJson(json['data']) : null;
}
}
class NestedAttributes {
String? name;
String? alternativeText;
String? caption;
int? width;
int? height;
Formats? formats;
String? hash;
String? ext;
String? mime;
double? size;
String? url;
Null? previewUrl;
String? provider;
Null? providerMetadata;
String? createdAt;
String? updatedAt;
NestedAttributes(
{this.name,
this.alternativeText,
this.caption,
this.width,
this.height,
this.formats,
this.hash,
this.ext,
this.mime,
this.size,
this.url,
this.previewUrl,
this.provider,
this.providerMetadata,
this.createdAt,
this.updatedAt});
NestedAttributes.fromJson(Map<String, dynamic> json) {
name = json['name'];
alternativeText = json['alternativeText'];
caption = json['caption'];
width = json['width'];
height = json['height'];
formats =
json['formats'] != null ? Formats.fromJson(json['formats']) : null;
hash = json['hash'];
ext = json['ext'];
mime = json['mime'];
size = json['size'];
url = json['url'];
previewUrl = json['previewUrl'];
provider = json['provider'];
providerMetadata = json['provider_metadata'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
}
}
class Formats {
Thumbnail? thumbnail;
Thumbnail? large;
Thumbnail? medium;
Thumbnail? small;
Formats({this.thumbnail, this.large, this.medium, this.small});
Formats.fromJson(Map<String, dynamic> json) {
thumbnail = json['thumbnail'] != null
? Thumbnail.fromJson(json['thumbnail'])
: null;
large = json['large'] != null ? Thumbnail.fromJson(json['large']) : null;
medium = json['medium'] != null ? Thumbnail.fromJson(json['medium']) : null;
small = json['small'] != null ? Thumbnail.fromJson(json['small']) : null;
}
}
class Thumbnail {
String? name;
String? hash;
String? ext;
String? mime;
int? width;
int? height;
double? size;
Null? path;
String? url;
Thumbnail(
{this.name,
this.hash,
this.ext,
this.mime,
this.width,
this.height,
this.size,
this.path,
this.url});
Thumbnail.fromJson(Map<String, dynamic> json) {
name = json['name'];
hash = json['hash'];
ext = json['ext'];
mime = json['mime'];
width = json['width'];
height = json['height'];
size = json['size'];
path = json['path'];
url = json['url'];
}
}
Solution 1:[1]
Try using response.data instead of response.body when you got the response.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ankit Kumar Maurya |
