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

时间:2020-04-16 08:06:06

标签: json parsing flutter dart

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

我的完整测验:{-M5-R3BqTajbCFk5mQuQ:{硬币:434,isSubmit:true, 问题:[{answer:sddsd,名称:我们为什么使用它?,选项: [建立,adwada,adawda,sddsd],选择:},{answer:adawda, 名称:我在哪里可以买到?,选项:[成立,adwada,adawda, sddsd],请选择:sddsd},{answer:adwada,名称:Lorem Ipsum就是 印刷文字?,选项:[已建立,adwada,adawda, sddsd],选择:建立}],quizId:YItWgbYjHm}, -M50HhYPnuR7tSC-9ajw:{isSubmit:true,问题:[{answer:dadada,名称:它来自哪里?,选项:[vvvv,dadada,dsdsdssd, bbbbbb],选择:dadada}],quizId:9pdzphz0x8}}

I / flutter(6768):类型'(dynamic,dynamic)=> Null'不是'f'类型'(dynamic)=> dynamic'的子类型

这里,数据库结构

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;
      }
    }

任何答案都会得到体现。

1 个答案:

答案 0 :(得分:0)

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

people