這是我想從 Flutter 客戶端發送到我的服務器的物件,但我無法:
{
"questions": [
{
"question" : "La velocidad de la luz es igual a",
"solucion1": "300k",
"solucion2": "250k",
"solucion3" : "150k",
"answer" : "300k"
},
{
"question" : "Un numero primo es",
"solucion1": "Solo puede ser dividido entre 1 y si mismo",
"solucion2": "Puede ser dividido entre 2",
"solucion3" : "Tiene raiz cuadrada",
"answer" : "Solo puede ser dividido entre 1 y si mismo"
},
{
"question" : "Las matematicas son",
"solucion1": "La ciencia que estudia la historia",
"solucion2": "La ciencia que estudia el oceano",
"solucion3" : "La ciencia que estudia la relacion entre los numeros",
"answer" : "La ciencia que estudia la relacion entre los numeros"
}
]
}
哪里"questions"可以有 N 量。現在讓我解釋一下我是如何嘗試構建它的。我正在使用一個類來處理如下問題:
class Questions {
String question = '';
String solucion1 = '';
String solucion2 = '';
String solucion3 = '';
String answer = '';
Questions(
{required this.answer,
required this.question,
required this.solucion1,
required this.solucion2,
required this.solucion3});
Questions.fromJson(Map<String, dynamic> json) {
question = json['question'];
solucion1 = json['solucion1'];
solucion2 = json['solucion2'];
solucion3 = json['solucion3'];
answer = json['answer'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['question'] = this.question;
data['solucion1'] = this.solucion1;
data['solucion2'] = this.solucion2;
data['solucion3'] = this.solucion3;
data['answer'] = this.answer;
return data;
}
}
在此之后,我嘗試使用其 3 個解決方案及其答案來構建每個問題,如下所示:
class Quiz {
List<Questions> questions = [];
Quiz({required this.questions});
Quiz.fromJson(Map<String, dynamic> json) {
if (json['questions'] != null) {
json['questions'].forEach((v) {
questions.add(new Questions.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.questions != []) {
data['questions'] = this.questions.map((v) => v.toJson()).toList();
}
return data;
}
}
每個問題將始終包含:1 個問題、3 個答案和 1 個正確答案。
現在,Widget Builder我有 2 個按鈕,1 個用于添加新問題,1 個用于創建測驗,意思是將資訊發送到服務器。
這是我用來添加新問題的按鈕:
CustomOutlinedButton(
onPressed: () {
newQuestion = Questions(
answer: answer,
question: question,
solucion1: solucion1,
solucion2: solucion2,
solucion3: solucion3);
newQuiz = Quiz.fromJson(newQuestion.toJson());
print(newQuiz.questions);
_optiona.clear();
_optionb.clear();
_optionc.clear();
_question.clear();
_answer.clear();
},
text: 'Agregar Pregunta')
當我嘗試列印newQuiz每個問題的值時,它只列印[].
這是將測驗發送到請求方法的按鈕:
CustomOutlinedButton(
onPressed: () async {
//print(data);
await createQuiz(
newQuiz.questions, teacherid, subjectid);
},
text: "Crear Quiz"),
現在在完整的 Widget Builder 代碼下方:
Widget build(BuildContext context) {
Questions newQuestion;
Quiz newQuiz = new Quiz(questions: []);
String question = "";
String solucion1 = "";
String solucion2 = "";
String solucion3 = "";
String answer = "";
return Scaffold(
appBar: AppBar(
title: Text("Crear un nuevo quiz de $subjectName"),
backgroundColor: Colors.green[700],
),
body: ChangeNotifierProvider(
create: (_) => LoginFormProvider(),
child: Builder(builder: (context) {
var _question = TextEditingController();
var _optiona = TextEditingController();
var _optionb = TextEditingController();
var _optionc = TextEditingController();
var _answer = TextEditingController();
final loginFormProvider =
Provider.of<LoginFormProvider>(context, listen: true);
return Container(
color: Colors.grey[100],
padding: EdgeInsets.symmetric(horizontal: 20),
child: Center(
child: ConstrainedBox(
constraints: BoxConstraints(maxHeight: 450),
child: Form(
key: loginFormProvider.formKey,
child: Column(
children: [
TextFormField(
onChanged: (value) => question = value,
controller: _question,
decoration: buildInputDecoration(
hint: 'Pregunta',
label: 'Pregunta',
icon: Icons.edit),
),
SizedBox(
height: 10,
),
TextFormField(
onChanged: (value) => solucion1 = value,
controller: _optiona,
decoration: buildInputDecoration(
hint: 'Opcion A',
label: 'Opcion A',
icon: Icons.edit),
),
SizedBox(
height: 10,
),
TextFormField(
onChanged: (value) => solucion2 = value,
controller: _optionb,
decoration: buildInputDecoration(
hint: 'Opcion B',
label: 'Opcion B',
icon: Icons.edit),
),
SizedBox(
height: 10,
),
TextFormField(
onChanged: (value) => solucion3 = value,
controller: _optionc,
decoration: buildInputDecoration(
hint: 'Opcion C',
label: 'Opcion C',
icon: Icons.edit),
),
SizedBox(
height: 10,
),
TextFormField(
onChanged: (value) => answer = value,
controller: _answer,
cursorColor: Colors.green,
decoration: buildInputDecoration(
hint: 'Respuesta',
label: 'Respuesta',
icon: Icons.edit),
),
SizedBox(
height: 15,
),
Row(children: [
CustomOutlinedButton(
onPressed: () async {
//print(data);
await createQuiz(
newQuiz.questions, teacherid, subjectid);
},
text: "Crear Quiz"),
SizedBox(
width: 20,
),
CustomOutlinedButton(
onPressed: () {
newQuestion = Questions(
answer: answer,
question: question,
solucion1: solucion1,
solucion2: solucion2,
solucion3: solucion3);
newQuiz = Quiz.fromJson(newQuestion.toJson());
//newQuiz.toJson();
print(newQuiz.questions);
_optiona.clear();
_optionb.clear();
_optionc.clear();
_question.clear();
_answer.clear();
},
text: 'Agregar Pregunta'),
])
],
),
),
),
));
})),
);
}
關于如何做到這一點的任何想法?我已經嘗試了很多東西,但我仍然得到列印為[].
uj5u.com熱心網友回復:
這部分不正確:
newQuiz = Quiz.fromJson(newQuestion.toJson());
想想看,newQuestion.toJson()會給你類似的東西:
{
"question" : "La velocidad de la luz es igual a",
"solucion1": "300k",
"solucion2": "250k",
"solucion3" : "150k",
"answer" : "300k"
}
但Quiz.fromJson()希望資料采用如下格式:
{
"questions": [
{
"question" : "La velocidad de la luz es igual a",
"solucion1": "300k",
"solucion2": "250k",
"solucion3" : "150k",
"answer" : "300k"
}
]
}
也許嘗試:
newQuiz = Quiz(questions: [newQuestion, ...newQuiz.questions]);
或者甚至只是這個:
newQuiz.questions.add(newQuestion);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359704.html
