我正在嘗試使用 http mathod“GET”從實時資料庫中獲取測驗資料。正在檢索資料,但它沒有顯示在串列視圖中,當我列印串列的長度時,它是 0。這就是我的終端中顯示的錯誤:錯誤:預期型別為“int”的值,但得到了“字串”型別之一
我無法弄清楚問題是什么。請幫我解決這個問題,因為我嘗試了大約 5 天,但無法解決。
謝謝你。
這些是我用于獲取串列中資料的代碼。
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);
});
}
}
這是我從實時資料庫中獲取的 JSON 格式的資料:
{
"Name" : "Miets Digital",
"Percent" : 100,
"Score" : 2,
"choice" : [ "Correct", "Correct" ],
"question" : [ {
"question" : "WHo is Elon Musk?"
}, {
"question" : "How did Elon musk got rich?"
} ]
}
uj5u.com熱心網友回復:
嘗試這個:-
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()回圈遍歷映射中的每個鍵值對。變數 key 和 value 為您提供回圈當前正在處理的鍵和值。如果要訪問地圖的值,則必須使用語法map['key_name'].
這也解釋了錯誤,因為您的Result()建構式需要一個整數但正在獲取一個字串值。
uj5u.com熱心網友回復:
決議 JSON 映射不是一個好習慣。你只需呼叫模型類
嘗試按照下面的代碼
Future<QuizModelResponse> getList() async {
// here write your code
var map = json.decode(response.body);
return QuizModelResponse.fromJsonMap(map);
}
串列回應類
class QuizModelResponse {
List<QuizItemModel> content;
QuizModelResponse.fromJsonMap(dynamic data) :
content = List<QuizItemModel>.from(
data.map((it) => QuizItemModel.fromJson(it)));
}
模型類
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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372211.html
