flutter - 为什么我不能将对象中的信息打印到控制台上
问题描述
为什么我不能在 dart 中打印以下语句:
print(questionBank[1].questionAnswer);
print(questionBank.length);
==================================================== ===
class Question {
String questionText;
bool questionAnswer;
Question({String ques, bool ans}) {
questionText = ques;
questionAnswer = ans;
}
}
void main{
List<Question> questionBank = [
Question(
ques: 'You can lead a cow down stairs but not up stairs.', ans: false),
Question(
ques: 'Approximately one quarter of human bones are in the feet.',
ans: true),
Question(ques: 'A slug\'s blood is green.', ans: true),
];
print(questionBank[1].questionAnswer);
print(questionBank.length);
}
解决方案
这是您的模型插入 toString 方法,通过右键单击您的模型>genrate>toString() '(Android studio)'
class Question {
String questionText;
bool questionAnswer;
Question({String ques, bool ans}) {
questionText = ques;
questionAnswer = ans;
}
@override
String toString() {
return 'Question{questionText: $questionText, questionAnswer: $questionAnswer}';
}
}
然后像这样使用:
debugPrint(questionBank[1].questionAnswer.toString());
推荐阅读
- .net - 使用具有强密码的 SSH.NET
- java - 将改造响应从一个类发送到一个活动
- python - 排序所需的多标签时间序列
- python - 我可以在没有 for 循环的情况下将函数应用于 Pandas 数据框中的多个列吗?
- python - 如何在 Python 中使用 NuGet 包?
- eclipse - 有没有办法在eclipse中切换搜索窗口结果
- version-control - 带有 MS SCC API 插件的文本编辑器
- javascript - 我可以“浏览”一个 csv 文件而不将所有数据存储在一个变量中吗?
- java - JAVA扫描器在使用bag时导致if语句问题
- palantir-foundry - 如何在 Foundry 转换中读取和写入列描述和类型类?