Home > Net >  Form TextField widget issue, The Form is sending data which is onScreen on submit is pressed
Form TextField widget issue, The Form is sending data which is onScreen on submit is pressed

Time:06-30

i am simply adding and showing Questions From Firestore Database,

I am adding Question in my Firestore Database, Every Textfield is getting updated in firestore database but the top 2 Textfield are returning no value, in my database it shows

QuestionId: "",

QuestionText: ""

All other fields field show data which was filled at the time of submission.

here is my code :

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:quizzy/data_controller.dart';
import 'package:quizzy/models/comman_dailog.dart';
import 'package:quizzy/models/product_image_picker.dart';

class AddQuestionScreen extends StatefulWidget {
  @override
  State<AddQuestionScreen> createState() => _AddQuestionScreenState();
}

class _AddQuestionScreenState extends State<AddQuestionScreen> {
  DataController controller = Get.find();

  final _formKey = GlobalKey<FormState>();
  // ignore: non_constant_identifier_names
  Map<String, dynamic> AddQuestionData = {
    "QuestionId": "",
    "QuestionText": "",
    "AnswerA": "",
    "AnswerB": "",
    "AnswerC": "",
    "AnswerD": "",
    "AskedIn": "",
    "SubjectName": "",
    "ChapterName": "",
    "CorrectAnswer": "",
  };
  var _userImageFile;

  void _pickedImage(File image) {
    _userImageFile = image;
  }

  addProduct() {
    _formKey.currentState!.save();
    if (_formKey.currentState!.validate()) {
      controller.addNewQuestion(AddQuestionData, _userImageFile);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).primaryColor,
      appBar: AppBar(
        centerTitle: true,
        title: Text('[Admin Only]Add New Question'),
      ),
      body: Card(
        child: Container(
          padding: const EdgeInsets.all(10),
          child: Form(
            key: _formKey,
            child: ListView(
              shrinkWrap: true,
              children: [
                const SizedBox(
                  height: 20,
                ),
                ProductImagePicker(_pickedImage),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'QuestionId ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'QustionId Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['QuestionId'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'Enter Question : QuestinonText ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'QuestionText Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['QuestionText'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'Answer A ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'Answer A Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['AnswerA'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'Answer B ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'AnswerB Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['AnswerB'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'Answer C ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'AnswerC Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['AnswerC'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'AnswerD ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'AnswerD Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['AnswerD'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'AskedIn ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'AskedIn Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['AskedIn'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'SubjectName ?  [Proper Spelling is Required]',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'SubjectName Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['SubjectName'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'ChapterName ?  [Proper Spelling is Required]',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'ChapterName Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['ChapterName'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                TextFormField(
                  controller: TextEditingController(),
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'CorrectAnswer ?  ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'CorrectAnswer Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['CorrectAnswer'] = value!;
                  },
                ),
                const SizedBox(
                  height: 20,
                ),
                ElevatedButton(
                  onPressed: addProduct,
                  child: const Text('Submit'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

You should define TextEditingController instance like below inside the widget class.

final textController1 = TextEditingController()
final textController2 = TextEditingController()
final textController3 = TextEditingController()

and set as value of controller. Do it for others too.


                TextFormField(
                  controller: textController1,
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'QuestionId ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'QustionId Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['QuestionId'] = textController1.text.trim();
                  },
                ),
TextFormField(
controller: textController2,
...


CodePudding user response:

Created 10 Controllers

  final textController1 = TextEditingController();
  final textController2 = TextEditingController();
  final textController3 = TextEditingController();
  final textController4 = TextEditingController();
  final textController5 = TextEditingController();
  final textController6 = TextEditingController();
  final textController7 = TextEditingController();
  final textController8 = TextEditingController();
  final textController9 = TextEditingController();
  final textController10 = TextEditingController();

doing this for all form fields

TextFormField(
                  controller: textController1,    // value of each TextController is different... in 2nd form field used different controller.
                  keyboardType: TextInputType.text,
                  decoration: const InputDecoration(
                    labelText: 'QuestionId ? ',
                  ),
                  validator: (value) {
                    if (value == null || value.isEmpty) {
                      return 'QustionId Required';
                    }
                    return null;
                  },
                  onSaved: (value) {
                    AddQuestionData['QuestionId'] = textController1.text.trim();    // same thing followed here also.
                  },
                ),

Thanks !

CodePudding user response:

Declare as follows: final textController = TextEditingController();

And then use it in TextFormField: TextFormField( controller: textController, .. )

  • Related