title: The non-nullable local variable 'result' must be assigned before it can be used
body: <p>I cannot find the solution to this problem. I am creating a sign up screen connected to firebase but it is giving me this error, &quot;The non-nullable local variable 'result' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path.&quot;</p>
  <p>The error is here. On the first and third line under result.</p>
  <pre><code>FirebaseFirestore.instance.collection(&quot;User&quot;).doc(result.user!.uid).set({
    &quot;UserName&quot;: userName.text,
    &quot;UserId&quot;: result.user!.uid,
    &quot;UserEmail&quot;: email.text,
    &quot;UserAddress&quot;: address.text,
    &quot;UserGender&quot;: isMale == true ? &quot;Male&quot; : &quot;Female&quot;,
    &quot;UserNumber&quot;: phoneNumber.text,
  });
  </code></pre>
  <p>This is the whole code</p>
  <pre><code>`import 'package:cloud_firestore/cloud_firestore.dart';
  import 'package:e_commerce_application/screens/homepage.dart';
  import 'package:e_commerce_application/screens/login.dart';
  import 'package:e_commerce_application/widgets/changescreen.dart';
  import 'package:e_commerce_application/widgets/mybutton.dart';
  import 'package:e_commerce_application/widgets/mytextformField.dart';
  import 'package:e_commerce_application/widgets/passwordtextformfield.dart';
  import 'package:firebase_auth/firebase_auth.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  
  class SignUp extends StatefulWidget {
    @override
    _SignUpState createState() =&gt; _SignUpState();
  }
  
  final GlobalKey&lt;FormState&gt; _formKey = GlobalKey&lt;FormState&gt;();
  final GlobalKey&lt;ScaffoldState&gt; _scaffoldKey = GlobalKey&lt;ScaffoldState&gt;();
  String p =
      r'^(([^&lt;&gt;()[\]\\.,;:\s@\&quot;]+(\.[^&lt;&gt;()[\]\\.,;:\s@\&quot;]+)*)|(\&quot;.+\&quot;))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
  
  RegExp regExp = new RegExp(p);
  bool obserText = true;
  final TextEditingController email = TextEditingController();
  final TextEditingController userName = TextEditingController();
  final TextEditingController phoneNumber = TextEditingController();
  final TextEditingController password = TextEditingController();
  final TextEditingController address = TextEditingController();
  
  bool isMale = true;
  bool isLoading = false;
  
  class _SignUpState extends State&lt;SignUp&gt; {
    void submit() async {
      UserCredential result;
      try {
        setState(() {
          isLoading = true;
        });
        result = await FirebaseAuth.instance.createUserWithEmailAndPassword(
            email: email.text, password: password.text);
        print(result);
      } on PlatformException catch (error) {
        var message = &quot;Please Check Your Internet Connection &quot;;
        if (error.message != null) {
          message = error.message!;
        }
        _scaffoldKey.currentState!.showSnackBar(SnackBar(
          content: Text(message.toString()),
          duration: Duration(milliseconds: 600),
          backgroundColor: Theme.of(context).primaryColor,
        ));
        setState(() {
          isLoading = false;
        });
      } catch (error) {
        setState(() {
          isLoading = false;
        });
        _scaffoldKey.currentState!.showSnackBar(SnackBar(
          content: Text(error.toString()),
          duration: Duration(milliseconds: 600),
          backgroundColor: Theme.of(context).primaryColor,
        ));
  
        print(error);
      }
      FirebaseFirestore.instance.collection(&quot;User&quot;).doc(result.user!.uid).set({
        &quot;UserName&quot;: userName.text,
        &quot;UserId&quot;: result.user!.uid,
        &quot;UserEmail&quot;: email.text,
        &quot;UserAddress&quot;: address.text,
        &quot;UserGender&quot;: isMale == true ? &quot;Male&quot; : &quot;Female&quot;,
        &quot;UserNumber&quot;: phoneNumber.text,
      });
      Navigator.of(context)
          .pushReplacement(MaterialPageRoute(builder: (ctx) =&gt; HomePage()));
      setState(() {
        isLoading = false;
      });
    }
  
    void vaildation() async {
      if (userName.text.isEmpty &amp;&amp;
          email.text.isEmpty &amp;&amp;
          password.text.isEmpty &amp;&amp;
          phoneNumber.text.isEmpty &amp;&amp;
          address.text.isEmpty) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;All Flied Are Empty&quot;),
          ),
        );
      } else if (userName.text.length &lt; 6) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Name Must Be 6 &quot;),
          ),
        );
      } else if (email.text.isEmpty) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Email Is Empty&quot;),
          ),
        );
      } else if (!regExp.hasMatch(email.text)) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Please Try Vaild Email&quot;),
          ),
        );
      } else if (password.text.isEmpty) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Password Is Empty&quot;),
          ),
        );
      } else if (password.text.length &lt; 8) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Password  Is Too Short&quot;),
          ),
        );
      } else if (phoneNumber.text.length &lt; 11 || phoneNumber.text.length &gt; 11) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Phone Number Must Be 11 &quot;),
          ),
        );
      } else if (address.text.isEmpty) {
        _scaffoldKey.currentState!.showSnackBar(
          SnackBar(
            content: Text(&quot;Address Is Empty &quot;),
          ),
        );
      } else {
        submit();
      }
    }
  
    Widget _buildAllTextFormField() {
      return Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: &lt;Widget&gt;[
            MyTextFormField(
              name: &quot;UserName&quot;,
              controller: userName,
            ),
            SizedBox(
              height: 10,
            ),
            MyTextFormField(
              name: &quot;Email&quot;,
              controller: email,
            ),
            SizedBox(
              height: 10,
            ),
            PasswordTextFormField(
              obserText: obserText,
              controller: password,
              name: &quot;Password&quot;,
              onTap: () {
                FocusScope.of(context).unfocus();
                setState(() {
                  obserText = !obserText;
                });
              },
            ),
            SizedBox(
              height: 10,
            ),
            GestureDetector(
              onTap: () {
                setState(() {
                  isMale = !isMale;
                });
              },
              child: Container(
                height: 60,
                padding: EdgeInsets.only(left: 10),
                width: double.infinity,
                decoration: BoxDecoration(border: Border.all(color: Colors.grey)),
                child: Center(
                  child: Row(
                    children: [
                      Text(
                        isMale == true ? &quot;Male&quot; : &quot;Female&quot;,
                        style: TextStyle(color: Colors.black87, fontSize: 18),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 10,
            ),
            MyTextFormField(
              name: &quot;Phone Number&quot;,
              controller: phoneNumber,
            ),
            SizedBox(
              height: 10,
            ),
            MyTextFormField(
              name: &quot;Address&quot;,
              controller: address,
            ),
          ],
        ),
      );
    }
  
    Widget _buildBottomPart() {
      return Container(
        margin: EdgeInsets.symmetric(horizontal: 10),
        width: double.infinity,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: &lt;Widget&gt;[
            _buildAllTextFormField(),
            SizedBox(
              height: 10,
            ),
            isLoading == false
                ? MyButton(
              name: &quot;SignUp&quot;,
              onPressed: () {
                vaildation();
              },
            )
                : Center(
              child: CircularProgressIndicator(),
            ),
            ChangeScreen(
              name: &quot;Login&quot;,
              whichAccount: &quot;I Have Already An Account!&quot;,
              onTap: () {
                Navigator.of(context).pushReplacement(
                  MaterialPageRoute(
                    builder: (ctx) =&gt; Login(),
                  ),
                );
              },
            ),
          ],
        ),
      );
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        key: _scaffoldKey,
        body: ListView(
          children: [
            Container(
              height: 200,
  
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: &lt;Widget&gt;[
                  Text(
                    &quot;Register&quot;,
                    style: TextStyle(
                      fontSize: 40,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              height: 500,
              child: _buildBottomPart(),
            ),
          ],
        ),
      );
    }
  }
  `
  </code></pre>