Дрожание на печати, показывающее вопрос - PullRequest
1 голос
/ 05 мая 2020

Я получаю данные из api, который работает, но проблема в том, что когда я печатаю значение, отображающее ошибку, если экземпляр вопроса

Main.dart

  var questions = new List<Questions>();

  _getQuestions() {
    API.getUsers().then((response) {
      setState(() {

        Iterable list = json.decode(response.body);
        print(list);
        print(list);
        questions = list.map((model) => Questions.fromJson(model)).toList();
        print(questions);
      });
    });
  }
  initState() {
    super.initState();
    _getQuestions();
  }

Модель

import 'package:flutter/material.dart';


class Questions {
  String would;
  String rather;
  int wouldClick;
  int ratherClick;

  Questions(int wouldclick, int ratherclick, String would, String rather) {
    this.wouldClick = wouldClick;
    this.ratherClick = ratherClick;
    this.would = would;
    this.rather = rather;
  }

  Questions.fromJson(Map json)
      : wouldClick = json['wouldClick'],
        ratherClick = json['ratherClick'],
        would = json['would'],
        rather = json['rather'];

  Map toJson() {
    return {'wouldClick': wouldClick, 'ratherClick': ratherClick, 'would': would, 'rather': rather};
  }

}

В консоли это отображается следующим образом

I/flutter ( 4808): [{rather: Tea, would: Coffe, wouldClick: 15, ratherClick: 12}, {rather: Oil, would: Soap, wouldClick: 50, ratherClick: 12}, {rather: Soap, would: Shaving Cream, wouldClick: 15, ratherClick: 12}]
I/flutter ( 4808): [Instance of 'Questions', Instance of 'Questions', Instance of 'Questions']

Мне нужно распечатать список вопросов в JSON, но он показывает этот пример ошибки вопросов

1 Ответ

2 голосов
/ 05 мая 2020

Вы должны переопределить метод toString в своем Question классе

Например

class Questions {
  String would;
  String rather;
  int wouldClick;
  int ratherClick;

  Questions(int wouldclick, int ratherclick, String would, String rather) {
    this.wouldClick = wouldClick;
    this.ratherClick = ratherClick;
    this.would = would;
    this.rather = rather;
  }

  Questions.fromJson(Map json)
      : wouldClick = json['wouldClick'],
        ratherClick = json['ratherClick'],
        would = json['would'],
        rather = json['rather'];

  Map toJson() {
    return {'wouldClick': wouldClick, 'ratherClick': ratherClick, 'would': would, 'rather': rather};
  }

  @override
  String toString() {
    return "Question(would: $would, rather: $rather, wouldClick: $wouldClick, ratherClick: $ratherClick)";
  }

}
...