title: Flutter textformfield validation with firebase database
body: <p>I am new to flutter and i am working on a result processing and record keeping system in the result processing part i have a form that takes in some details about the students like session, level, course code(It's for a university). now i have a collection in firebase of the levels and sessions in the department i am using for a case study and would like to check that the lecturer input corresponds or matches what is in the database.I am using a textformfield but i don't know how to validate the input with what is in firebase database. This is my code for the form. Thanks in advance for the help</p>
  <pre><code>    class ScoreSheetPage extends StatefulWidget {
        @override
        State&lt;ScoreSheetPage&gt; createState() =&gt; _ScoreSheetPageState();
      }
      
      class _ScoreSheetPageState extends State&lt;ScoreSheetPage&gt; {
        final formKey = GlobalKey&lt;FormState&gt;();
      
        TextEditingController currentSession = TextEditingController();
      
        TextEditingController currentLevelYear = TextEditingController();
      
        TextEditingController matriculationNumber = TextEditingController();
      
        TextEditingController currentSemester = TextEditingController();
      
        TextEditingController courseCode = TextEditingController();
      
        TextEditingController studentMarks1 = TextEditingController();
        TextEditingController studentMarks2 = TextEditingController();
        TextEditingController studentMarks3 = TextEditingController();
        TextEditingController studentMarks4 = TextEditingController();
      
        
        @override
        Widget build(BuildContext context) {
          final double height = MediaQuery.of(context).size.height;
          final GlobalKey&lt;ScaffoldState&gt; _scaffoldKey = GlobalKey&lt;ScaffoldState&gt;();
          return Scaffold(
            backgroundColor: Colors.white,
            body: SafeArea(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Stack(
                      children: [
                        Container(
                          height: 230,
                          decoration: BoxDecoration(color: Color(0xFF363f93)),
                        ),
                        SizedBox(
                          height: 100,
                        ),
                        Center(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Container(
                                alignment: Alignment.bottomCenter,
                                child: Text(
                                  &quot;RHEMA UNIVERSITY RESULT CALCULATION SYSTEM&quot;,
                                  textAlign: TextAlign.center,
                                  style: kHeading,
                                ),
                              ),
                              Text(
                                &quot;Welcome&quot;,
                                textAlign: TextAlign.center,
                                style: kHeading,
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                    Container(
                        alignment: Alignment.topLeft,
                        child: Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: Form(
                            key: formKey,
                            child: Column(
                              children: [
                                Text(
                                  &quot;Please fill in the required information&quot;,
                                  style: TextStyle(
                                    color: Color(0xFF363f93),
                                    fontSize: 30,
                                    fontWeight: FontWeight.bold,
                                  ),
                                  textAlign: TextAlign.start,
                                ),
                                SizedBox(
                                  height: height * 0.04,
                                ),
                                TextFormField(
                                  controller: currentSession,
                                  decoration: InputDecoration(
                                      labelText:
                                          &quot;Enter current session, use format(1234-1234)&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^([0-9]{4}[-][0-9]{4})+$')
                                            .hasMatch(value)) {
                                      return &quot;Enter correct session number&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                
                                ),
                                SizedBox(
                                  height: height * 0.04,
                                ),
                                TextFormField(
                                  controller: currentLevelYear,
                                  decoration: InputDecoration(
                                      labelText: &quot;Enter Current Level/year&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^([0-9]{3})+$').hasMatch(value)) {
                                      return &quot;Enter correct level and year&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.04,
                                ),
                                TextFormField(
                                  controller: matriculationNumber,
                                  decoration: InputDecoration(
                                      labelText:
                                          &quot;Enter student's matriculation number, use format(RU-CSC-19-202)&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^[A-Z]{2}[-][A-Z]{3}[-][0-9]{2}[-][0-9]{3}')
                                            .hasMatch(value)) {
                                      return &quot;Enter correct Matriculation Number&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.04,
                                ),
                                TextFormField(
                                  controller: currentSemester,
                                  decoration: InputDecoration(
                                      labelText: &quot;Enter current semester&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^[a-z,A-Z]+$').hasMatch(value)) {
                                      return &quot;Enter correct name&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.04,
                                ),
                                TextFormField(
                                  controller: studentMarks1,
                                  decoration: InputDecoration(
                                      labelText: &quot;Enter score for attendance&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^[0-9]+$').hasMatch(value)) {
                                      return &quot;Enter correct score&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.05,
                                ),
                                TextFormField(
                                  controller: studentMarks2,
                                  decoration: InputDecoration(
                                      labelText: &quot;Enter score for assignment&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^[0-9]+$').hasMatch(value)) {
                                      return &quot;Enter correct score&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.05,
                                ),
                                TextFormField(
                                  controller: studentMarks3,
                                  decoration: InputDecoration(
                                      labelText: &quot;Enter score for test&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^[0-9]+$').hasMatch(value)) {
                                      return &quot;Enter correct score&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.05,
                                ),
                                TextFormField(
                                  controller: studentMarks4,
                                  decoration: InputDecoration(
                                      labelText: &quot;Enter Score for exams&quot;),
                                  validator: (value) {
                                    if (value!.isEmpty ||
                                        !RegExp(r'^[0-9]+$').hasMatch(value)) {
                                      return &quot;Enter correct score&quot;;
                                    } else {
                                      return null;
                                    }
                                  },
                                ),
                                SizedBox(
                                  height: height * 0.05,
                                ),
                                GestureDetector(
                                  child: Container(
                                      alignment: Alignment.center,
                                      width: 500,
                                      child: SizedBox(
                                        height: 50,
                                        width: double.infinity,
                                        child: ElevatedButton(
                                            style: ButtonStyle(
                                              foregroundColor:
                                                  MaterialStateProperty.all&lt;Color&gt;(
                                                      Colors.black),
                                              backgroundColor:
                                                  MaterialStateProperty.all(
                                                      Colors.black),
                                              shape: MaterialStateProperty.all&lt;
                                                      RoundedRectangleBorder&gt;(
                                                  RoundedRectangleBorder(
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              1000))),
                                            ),
                                            child: Text(&quot;Submit&quot;, style: kHeading),
                                            onPressed: () {
                                              if (formKey.currentState!.validate()) {
                                                
                                              
  var finalScore = int.parse(
                                                        studentMarks2.text) +
                                                    int.parse(studentMarks3.text) +
                                                    int.parse(studentMarks4.text);
                                                Map&lt;String, dynamic&gt; data = {
                                                  &quot;Attendance&quot;: studentMarks1.text,
                                                  &quot;Assignment&quot;: studentMarks2.text,
                                                  &quot;Test Score&quot;: studentMarks3.text,
                                                  &quot;Examination Score&quot;:
                                                      studentMarks4.text,
                                                  &quot;final score&quot;: finalScore
                                                };
                                                final docUser = FirebaseFirestore
                                                    .instance
                                                    .collection(&quot;Exam Scores&quot;)
                                                    .doc(
                                                        &quot;${currentLevelYear.text} Level&quot;)
                                                    .collection(
                                                        matriculationNumber.text)
                                                    .doc(
                                                        &quot;${currentSemester.text} semester&quot;)
                                                    .collection(courseCode.text)
                                                    .doc(&quot;Scores&quot;);
                                                docUser.set(data);
                                              }
                                            }),
                                      )),
                                ),
                                SizedBox(
                                  height: 15,
                                ),
                                TextButton(
                                    onPressed: () {
                                      Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =&gt; Homepage()));
                                    },
                                    child: Text(
                                      &quot;Go back to home page&quot;,
                                      style: TextStyle(
                                        fontSize: 25,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    )),
                              ],
                            ),
                          ),
                        )),
                  ],
                ),
              ),
            ),
          );
        }
      }
  </code></pre>