gpt4 book ai didi

json - flutter-类型 '(dynamic, dynamic) => Null'不是 '(dynamic) => dynamic'的 'f'类型的子类型

转载 作者:行者123 更新时间:2023-12-03 03:56:02 27 4
gpt4 key购买 nike

我只是尝试从Firebase实时数据库解析数据。
但在转换为模型时有问题
我正在尝试从Flutter上解析Firebase数据库中的数据。
但是一个错误说

MY Complete QUIZ: {-M5-R3BqTajbCFk5mQuQ: {coins: 434, isSubmit: true,questions: [{answer: sddsd, name: Why do we use it?, options:[established, adwada, adawda, sddsd], select: }, {answer: adawda,name: Where can I get some?, options: [established, adwada, adawda,sddsd], select: sddsd}, {answer: adwada, name: Lorem Ipsum is simplydummy text of the printing?, options: [established, adwada, adawda,sddsd], select: established}], quizId: YItWgbYjHm},-M50HhYPnuR7tSC-9ajw: {isSubmit: true, questions: [{answer: dadada, name: Where does it come from?, options: [vvvv, dadada, dsdsdssd,bbbbbb], select: dadada}], quizId: 9pdzphz0x8}}

I/flutter ( 6768): type '(dynamic, dynamic) => Null' is not a subtype of type '(dynamic) => dynamic' of 'f'


在这里,数据库结构
enter image description here fetchMyQuiz()的以下功能
     Future<Quiz> fetchMyQuiz(String uid) async {
Quiz _quiz;

var dio = Dio();
dio.options
..baseUrl = Constant.baseUrl
..connectTimeout = 5000 //5s
..receiveTimeout = 5000
..validateStatus = (int status) {
return status > 0;
}
..headers = {
HttpHeaders.userAgentHeader: 'dio',
'common-header': 'xx',
};

_isLoadingUser = true;
notifyListeners();

List<Quiz> _fetchedQuiz = [];

try {
var response = await dio.get(
Constant.userParam + '/$uid' + Constant.quiz + Constant.jsonExt,
options: Options(
contentType: Headers.formUrlEncodedContentType,
),
);

print("MY Complete QUIZ: ${response.data}");

if (response.statusCode == 200) {
var responseData = response.data;
responseData.forEach((String id, dynamic json) {
if (responseData != null) {
_quiz = Quiz.fromJson(id, json);
_fetchedQuiz.add(_quiz);
}
});
} else {
print("FETCH QUIZ error: ${response.data}");
}
} catch (e) {
print(e);
}

_myQuizList = _fetchedQuiz;

_isLoadingUser = false;
notifyListeners();

return _quiz;
}

class Quiz {
String id;
String quizId;
int coins;
bool isSubmit;
List<Questions> questions;

Quiz({this.id, this.quizId, this.coins, this.isSubmit, this.questions});

Quiz.fromJson(String idQuiz, Map<String, dynamic> json) {
id = idQuiz;
quizId = json['quizId'];
coins = json['coins'];
isSubmit = json['isSubmit'] == null ? false : json['isSubmit'];
if (json['questions'] != null) {
questions = new List<Questions>();
json['questions'].forEach((idQuest, vQuest) {
questions.add(new Questions.fromJson(idQuest, vQuest));
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['quizId'] = this.quizId;
data['coins'] = this.coins;
data['isSubmit'] = this.isSubmit;
if (this.questions != null) {
data['questions'] = this.questions.map((v) => v.toJson()).toList();
}
return data;
}
}

class Questions {
String id;
String name;
String select;
String answer;
// bool isSave;
List<String> options;

Questions(
{this.id,
this.name,
this.select,
this.answer,
// this.isSave,
this.options});

Questions.fromJson(String id, Map<String, dynamic> json) {
id = id;
name = json['name'];
select = json['select'] == null ? '' : json['select'];
answer = json['answer'];
// isSave = false;
if (json['options'] != null) {
options = json['options'].cast<String>();
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['select'] = this.select;
data['answer'] = this.answer;
// data['isSave'] = this.isSave;
data['options'] = this.options;
return data;
}
}
任何答案都会得到体现。

最佳答案

我认为您的问题出在解析过程中。我使用quicktype生成了模型。在下面检查

class Quiz {
final int coins;
final bool isSubmit;
final List<Question> questions;
final String quizId;

Quiz({
this.coins,
this.isSubmit,
this.questions,
this.quizId,
});

Quiz copyWith({
int coins,
bool isSubmit,
List<Question> questions,
String quizId,
}) =>
Quiz(
coins: coins ?? this.coins,
isSubmit: isSubmit ?? this.isSubmit,
questions: questions ?? this.questions,
quizId: quizId ?? this.quizId,
);

factory Quiz.fromRawJson(String str) => Quiz.fromJson(json.decode(str));

String toRawJson() => json.encode(toJson());

factory Quiz.fromJson(Map<String, dynamic> json) => Quiz(
coins: json["coins"] == null ? null : json["coins"],
isSubmit: json["isSubmit"] == null ? null : json["isSubmit"],
questions: json["questions"] == null ? null : List<Question>.from(json["questions"].map((x) => Question.fromJson(x))),
quizId: json["quizId"] == null ? null : json["quizId"],
);

Map<String, dynamic> toJson() => {
"coins": coins == null ? null : coins,
"isSubmit": isSubmit == null ? null : isSubmit,
"questions": questions == null ? null : List<dynamic>.from(questions.map((x) => x.toJson())),
"quizId": quizId == null ? null : quizId,
};
}

class Question {
final String answer;
final String name;
final List<String> options;
final dynamic select;

Question({
this.answer,
this.name,
this.options,
this.select,
});

Question copyWith({
String answer,
String name,
List<String> options,
dynamic select,
}) =>
Question(
answer: answer ?? this.answer,
name: name ?? this.name,
options: options ?? this.options,
select: select ?? this.select,
);

factory Question.fromRawJson(String str) => Question.fromJson(json.decode(str));

String toRawJson() => json.encode(toJson());

factory Question.fromJson(Map<String, dynamic> json) => Question(
answer: json["answer"] == null ? null : json["answer"],
name: json["name"] == null ? null : json["name"],
options: json["options"] == null ? null : List<String>.from(json["options"].map((x) => x)),
select: json["select"],
);

Map<String, dynamic> toJson() => {
"answer": answer == null ? null : answer,
"name": name == null ? null : name,
"options": options == null ? null : List<dynamic>.from(options.map((x) => x)),
"select": select,
};
}

关于json - flutter-类型 '(dynamic, dynamic) => Null'不是 '(dynamic) => dynamic'的 'f'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com