Don't know how to resolve it. The two lines show error. It should be resolved to run the code.
I got error on the two lines where type id and names are converted into "id" and "names".
// To parse this JSON data, do
//
// final welcome = welcomeFromJson(jsonString);
import 'dart:convert';
Welcome welcomeFromJson(String str) => Welcome.fromJson(json.decode(str));
String welcomeToJson(Welcome data) => json.encode(data.toJson());
class Welcome {
Welcome({
required this.status,
required this.totalResults,
required this.articles,
});
String status;
int totalResults;
List<Article> articles;
factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
status: json["status"],
totalResults: json["totalResults"],
articles: List<Article>.from(
json["articles"].map((x) => Article.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"status": status,
"totalResults": totalResults,
"articles": List<dynamic>.from(articles.map((x) => x.toJson())),
};
}
class Article {
Article({
required this.source,
required this.author,
required this.title,
required this.description,
required this.url,
required this.urlToImage,
required this.publishedAt,
required this.content,
});
Source source;
String author;
String title;
String description;
String url;
String urlToImage;
DateTime publishedAt;
String content;
factory Article.fromJson(Map<String, dynamic> json) => Article(
source: Source.fromJson(json["source"]),
author: json["author"] == null ? null : json["author"],
title: json["title"],
description: json["description"],
url: json["url"],
urlToImage: json["urlToImage"],
publishedAt: DateTime.parse(json["publishedAt"]),
content: json["content"],
);
Map<String, dynamic> toJson() => {
"source": source.toJson(),
"author": author == null ? null : author,
"title": title,
"description": description,
"url": url,
"urlToImage": urlToImage,
"publishedAt": publishedAt.toIso8601String(),
"content": content,
};
}
class Source {
Source({
required this.id,
required this.name,
});
Id id;
Name name;
factory Source.fromJson(Map<String, dynamic> json) => Source(
id: idValues.map[json["id"]],
name: nameValues.map[json["name"]],
);
Map<String, dynamic> toJson() => {
"id": idValues.reverse[id],
"name": nameValues.reverse[name],
};
}
enum Id { THE_WALL_STREET_JOURNAL }
final idValues =
EnumValues({"the-wall-street-journal": Id.THE_WALL_STREET_JOURNAL});
enum Name { THE_WALL_STREET_JOURNAL }
final nameValues =
EnumValues({"The Wall Street Journal": Name.THE_WALL_STREET_JOURNAL});
class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
I think if we parse them then it can be resolved but I don't know how to parse it.
CodePudding user response:
Change these codes :
Id id;
Name name;
to this :
Id? id;
Name? name;
CodePudding user response:
You have set Id to be required, yet your code does not guarantee that what you are parsing is going to actually have a Id value that is not null because you are using a Map that has dynamic values, which means they can be anything, including null. In other words: you cannot assign a nullable Id (the "?" behind "Id?" means it's nullable) to an Id that is not nullable.
Without running your code, some suggestions that might help is:
- Make your Id property nullable
- Ensure that the value is not null where you are trying to assign the id by using a "!". The "!" means that you are guaranteeing the value is not null.
- Use the "late" keyword to initialize the value after it's declaration.
Be warned though: The two latter suggestions will cause exceptions if you cannot guarantee that the value is not null, defeating the whole purpose of null safety.