flutter - type '(dynamic, dynamic) => Null' is not a subtype of type '(dynamic) => dynamic' of 'f'
I think your problem is in your parsing process. I used quicktype to generate your models. check them below
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,
};
}
Muhammad Imanudin
Updated on December 19, 2022Comments
-
Muhammad Imanudin over 1 year
I just tried parsing data from a firebase realtime database. but has problems when converting to Model I'm trying to parse data from the firebase database on Flutter. But an error said
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 simply dummy 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'
Here, the database structure
The following function for
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; } }
Any answer will appretiated.
-
Payam Zahedi about 4 yearsI think this is a cast exception. check the runtimeType of your data like this print(response.data.runtimeType);. also, log stack of your error.
-
Muhammad Imanudin about 4 years@PayamZahedi when i check it says RunType
_InternalLinkedHashMap<String, dynamic>
-
Muhammad Imanudin about 4 yearsI have tried various methods such as
Map<String, dynamic>.from(json)
or<String, dyanmic>.cast (json)
but still doesn't work -
Payam Zahedi about 4 yearsplease share your JSON result and stack too.
-
Muhammad Imanudin about 4 yearsim added it in questions @PayamZahedi
-