I am trying to fetch quizzes data from realtime database using http mathod "GET". The data is being retreived but it's not showing up in listview and when i print the length of my list it's 0. And this is what the error is being shown in my terminal: Error: Expected a value of type 'int', but got one of type 'String'
I can't figure out what the problem is. Please help me solve this problem because i am trying for around 5 days but can't solve it.
Thank you.
These are my codes for fetching the data in lists.
Future<void> getList() async {
list.clear();
final url = Uri.parse(
'https://testisnotesttheisthelearningapp-default-rtdb.firebaseio.com/Schools/${widget.uid}/ResultsFolder/${widget.id}.json');
final response = await get(url);
print(response.body);
var map = json.decode(response.body) as Map<String, dynamic>;
// print(map);
if (map != null) {
map.forEach((key, value) {
print(value['Name']);
var temp = Result(
value['Percent'],
(value['choice'] as List<dynamic>)
.map((e) => Model(e['index'], e['question'], []))
.toList(),
value['Score'],
(value['question'] as List<dynamic>).map((e) => e).toList(),
value['Name']);
list.add(temp);
});
}
}
This is my data from real-time database in JSON format:
{
"Name" : "Miets Digital",
"Percent" : 100,
"Score" : 2,
"choice" : [ "Correct", "Correct" ],
"question" : [ {
"question" : "WHo is Elon Musk?"
}, {
"question" : "How did Elon musk got rich?"
} ]
}
CodePudding user response:
Try this:-
Future<void> getList() async {
list.clear();
final url = Uri.parse(
'https://testisnotesttheisthelearningapp-default-rtdb.firebaseio.com/Schools/${widget.uid}/ResultsFolder/${widget.id}.json');
final response = await get(url);
print(response.body);
var map = json.decode(response.body) as Map<String, dynamic>;
// print(map);
if (map != null) {
print(map['Name']);
var temp = Result(
map['Percent'],
(map['choice'] as List<dynamic>)
.map((e) => Model(e['index'], e['question'], []))
.toList(),
map['Score'],
(map['question'] as List<dynamic>).map((e) => e).toList(),
map['Name']);
list.add(temp);
}
}
forEach()
loop goes through each key value pair in a map. The variables key and value gives you the key and the value the loop is currently working on. If you want to access the values of a map you will have to use the syntax map['key_name']
.
This also explains the error, as your Result()
constructor is expecting an integer but is getting a string value.
CodePudding user response:
It's not a good practice to parse JSON maps. You just call over the model class
Try to follow the below code
Future<QuizModelResponse> getList() async {
// here write your code
var map = json.decode(response.body);
return QuizModelResponse.fromJsonMap(map);
}
List Response class
class QuizModelResponse {
List<QuizItemModel> content;
QuizModelResponse.fromJsonMap(dynamic data) :
content = List<QuizItemModel>.from(
data.map((it) => QuizItemModel.fromJson(it)));
}
Model class
class QuizItemModel {
String name;
int percent;
int score;
List<String> choice;
List<Question> question;
QuizItemModel(
{this.name, this.percent, this.score, this.choice, this.question});
QuizItemModel.fromJson(Map<String, dynamic> json) {
name = json['Name'];
percent = json['Percent'];
score = json['Score'];
choice = json['choice'].cast<String>();
if (json['question'] != null) {
question = new List<Question>();
json['question'].forEach((v) {
question.add(new Question.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Name'] = this.name;
data['Percent'] = this.percent;
data['Score'] = this.score;
data['choice'] = this.choice;
if (this.question != null) {
data['question'] = this.question.map((v) => v.toJson()).toList();
}
return data;
}
}
class Question {
String question;
Question({this.question});
Question.fromJson(Map<String, dynamic> json) {
question = json['question'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['question'] = this.question;
return data;
}
}