gpt4 book ai didi

flutter - flutter ,显示问题实例

转载 作者:行者123 更新时间:2023-12-03 03:22:33 25 4
gpt4 key购买 nike

我从正在工作的api获取数据,但是问题是当我打印值时如果问题实例显示错误

主镖

  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中打印问题列表,但显示此问题实例错误

最佳答案

您应该在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)";
}

}

关于flutter - flutter ,显示问题实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61613681/

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