'Email verification cause error on flutter? why?

I'm developing a food ordering app for that i want to verify the user through email. For that i'm using email verification. i'm using firebase E-mail verification. come to my point i verified the email after verify the email error is founded on flutter. i'm developed the app on flutter. kindly provide some steps to avoid error. I attached the error message below

E/flutter ( 9906): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
E/flutter ( 9906): #0      _verifiyState.checkmail (package:datafound/vvvv.dart:31:44)
E/flutter ( 9906): #1      _verifiyState.initState.<anonymous closure> (package:datafound/vvvv.dart:26:55)
E/flutter ( 9906): #2      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter ( 9906): #3      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 9906): #4      _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 9906): #5      _CustomZone.bindUnaryCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1281:26)
E/flutter ( 9906): #6      _rootRunUnary (dart:async/zone.dart:1444:13)
E/flutter ( 9906): #7      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 9906): #8      _CustomZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1265:26)
E/flutter ( 9906): #9      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 9906): #10     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 9906): #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 9906): 

this error is showing after email verified and here my code


import 'package:datafound/vvvv.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class Emailver extends StatefulWidget {
  const Emailver({Key? key}) : super(key: key);

  @override
  _EmailverState createState() => _EmailverState();
}

class _EmailverState extends State<Emailver> {
  TextEditingController emailCtrl = TextEditingController();
  TextEditingController passCtrl = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("email verification"),
      ),
      body: Center(
        child: Column(
          children: [
            const SizedBox(
              height: 10.0,
            ),
            TextFormField(
              controller: emailCtrl,
              keyboardType: TextInputType.emailAddress,
              decoration: InputDecoration(
                border:OutlineInputBorder(
                  borderSide: const BorderSide(width: 1, color: Colors.black),
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
            ),
            const SizedBox(
              height: 10.0,
            ),
            TextFormField(
              controller: passCtrl,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                border:OutlineInputBorder(
                  borderSide: const BorderSide(width: 1, color: Colors.black),
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
            ),
            const SizedBox(
              height: 10.0,
            ),
            ElevatedButton(onPressed: (){
              signup();
            },
                child: const Text("Sign Up"))
          ],
        ),
      ),
    );
  }

  Future signup() async{
    await FirebaseAuth.instance.createUserWithEmailAndPassword(email: emailCtrl.text, password: passCtrl.text).then((_){
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => const verifiy()),
      );
    });
  }

}

the above code is for getting email from user




import 'dart:async';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class verifiy extends StatefulWidget {


  const verifiy({Key? key}) : super(key: key);

  @override
  _verifiyState createState() => _verifiyState();
}

class _verifiyState extends State<verifiy> {
  bool isvv = false;
  Timer? timer;

  @override
  void initState() {
    isvv = FirebaseAuth.instance.currentUser!.emailVerified;
    !isvv ? sendnotfy() : null;
    Timer.periodic(const Duration(seconds: 3), (_) => checkmail(),);
    super.initState();
  }

  Future checkmail() async{
    await FirebaseAuth.instance.currentUser!.reload();
      setState(() {
        isvv = FirebaseAuth.instance.currentUser!.emailVerified;
      });

      if(isvv) timer?.cancel();
  }

    Future sendnotfy() async{
       final user = FirebaseAuth.instance.currentUser!;
       await user.sendEmailVerification();
    }


    @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
              appBar: AppBar(
                title: const Text("verify"),
              ),
              body:  isvv ? Column(
                children: [
                  const Text("verifiy your email"),
                  ElevatedButton(
                    onPressed: (){
                      FirebaseAuth.instance.signOut().then((_){
                        Navigator.pop(context);
                      });
                    },
                    child: const Text("sign out"),
                  )
                ],
              ) :  Center(
                child: Container(
                  color: Colors.red,
                  child: const Text("Check Your Mail and click it"),
                ),
              ) ,
            );
  }
}

the above for email verification please provide solution as soon as possible. Thank you



Solution 1:[1]

You should check null if your statement nullable. Nullable expression "!" If you use it you will get this error.

import 'dart:async';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class verifiy extends StatefulWidget {


  const verifiy({Key? key}) : super(key: key);

  @override
  _verifiyState createState() => _verifiyState();
}

class _verifiyState extends State<verifiy> {
  bool isvv = false;
  Timer? timer;

  @override
  void initState() {
  final currentUser = FirebaseAuth.instance.currentUser;
  if (currentUser != null) {
    final isEmailVerifed = currentUser!.emailVerified;
    if (!isEmailVerifed) {
      sendnotfy()
    }
  }
  Timer.periodic(const Duration(seconds: 3), (_) => checkmail(),);
  super.initState();
  }

Future checkmail() async {
  final currentUser = FirebaseAuth.instance.currentUser;
  var isvv = false;
  if (currentUser != null) {
    await currentUser.reload();
    setState(() {
      isvv = currentUser.emailVerified;
    });
  }

  if (isvv) timer?.cancel();
}

Future sendnotfy() async {
  final currentUser = FirebaseAuth.instance.currentUser;
  if (currentUser != null) {
    await currentUser.sendEmailVerification();
  }
}


    @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
              appBar: AppBar(
                title: const Text("verify"),
              ),
              body:  isvv ? Column(
                children: [
                  const Text("verifiy your email"),
                  ElevatedButton(
                    onPressed: (){
                      FirebaseAuth.instance.signOut().then((_){
                        Navigator.pop(context);
                      });
                    },
                    child: const Text("sign out"),
                  )
                ],
              ) :  Center(
                child: Container(
                  color: Colors.red,
                  child: const Text("Check Your Mail and click it"),
                ),
              ) ,
            );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Salih Can