【问题标题】:Error: Expected a value of type 'int', but got one of type 'String'; flutter错误:需要一个“int”类型的值,但得到一个“String”类型的值;扑
【发布时间】:2022-01-08 18:42:28
【问题描述】:

我正在尝试使用 http mathod“GET”从实时数据库中获取测验数据。 正在检索数据,但它没有显示在 listview 中,当我打印列表的长度时,它为 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?"
  } ]
}

【问题讨论】:

    标签: firebase flutter http listview firebase-realtime-database


    【解决方案1】:

    试试这个:-

    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() 构造函数需要一个整数但得到一个字符串值。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-09-13
        • 2021-12-27
        • 2021-09-18
        • 2021-03-16
        • 2020-12-03
        • 2021-11-02
        • 2021-10-23
        • 1970-01-01
        • 2021-10-31
        相关资源
        最近更新 更多