Help me to remove the conflict bw flutter_login animation and StreamProvider

  1. Summary of my problem
    Please try to give me the solution, I want to do this but I am missing some concept! : )
  • Details about my goal

I am creating a sign in/signup screen using flutter_login package which gives inbuilt animation and for authentication, I am using Firebase, and for listening to the auth changes stream which is basically SteamProvider.value()

  • Expected Results
    When the uses the first-time login, I want to show the login screen, and after the login screen’s animation completed, the home screen will come and if the user again use the app, if he has not logged out than I want to show him/her Home() Screen

  • Actual Results
    The stream is listening so fast that it rebuilding the tree too fast so without animation completed and I want to stop that.

  • Error Messages (if any)
    Not so much but the dispose() method called before the animation complete.

  1. What have I tried so far -
    almost, whatever if-else conditions are possible, I have tried and sometimes I got the required result but still its accuracy not so good.
  2. Here’s the minimum code you would need to reproduce the problem -
    Starting of APP
import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:serviceapp/screens/wrapper.dart';

import 'package:serviceapp/services/auth.dart';

class MyApp extends StatelessWidget {

  // This widget is the root of your application.

  @override

  Widget build(BuildContext context) {

    return StreamProvider.value(

      value: AuthService().user,

      child: MaterialApp(

        title: 'ServiceApp',

        debugShowCheckedModeBanner: false,

        theme: ThemeData(

          primarySwatch: Colors.blue,

          visualDensity: VisualDensity.adaptivePlatformDensity,

        ),

        home: Wrapper(),

      ),

    );

  }

}

Wrapper for management

import 'package:flutter_login/flutter_login.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'package:serviceapp/models/customUser.dart';
import 'package:serviceapp/screens/home/home.dart';
import 'package:serviceapp/services/auth.dart';

class Wrapper extends StatefulWidget {
  @override
  _WrapperState createState() => _WrapperState();
}

enum STATE { Loggin, Logout }

var state = STATE.Loggin;

class _WrapperState extends State<Wrapper> {
  // bool _authCompletion;
  bool isUserSignedIn;
  final _auth = AuthService();
  Widget output = Center(child: CircularProgressIndicator());
  @override
  Widget build(BuildContext context) {
    final customeUser = Provider.of<CustomUser>(context);
    isUserSignedIn = customeUser != null;
    print(isUserSignedIn);
    print(state);
    Future.delayed(const Duration(seconds: 2), () {
      print('future function');
      if (!isUserSignedIn) {
        setState(() {
          state = STATE.Logout;
          output = FlutterLogin(
            onSignup: _auth.createUserWithEmailAndPassword,
            onLogin: _auth.signInWithEmailAndPassword,
            onRecoverPassword: (_) => Future(null),
            onSubmitAnimationCompleted: () {
              setState(() {
                state = STATE.Loggin;
              });
              print("signin Completed");
            },
            title:' Chapter',
            logo: 'assets/images/ic_launcher.png',
            theme: LoginTheme(
                accentColor: Colors.white,
                titleStyle: GoogleFonts.cinzel(
                  fontWeight: FontWeight.w100,
                  fontSize: 40,
                )),
          );
        });
      } else if (state == STATE.Logout) {
        setState(() {
          state = STATE.Loggin;
          output = Home();
        });
      } else {
        setState(() {
          output = Home();
        });
      }
    });
    print(output);
    // print(isUserSignedIn);
    return output;
  }
}

auth services for connection

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_login/flutter_login.dart';
import 'package:serviceapp/models/customUser.dart';

class AuthService {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
  //Create User object base on firebase object
  String errorCode = '';
  CustomUser _currentUser;

  CustomUser _userFromFirebaseUser(User user) {
    if (user != null) {
      return CustomUser(
        uid: user.uid,
        displayName: user.displayName,
        email: user.email,
      );
    } else {
      return null;
    }
  }

  // Signin Annon
  Future signInAnnon() async {
    try {
      UserCredential userCredential = await _firebaseAuth.signInAnonymously();
      print(userCredential.user.toString());
      return _userFromFirebaseUser(userCredential.user);
    } on FirebaseAuthException catch (error) {
      print(error.code);
      errorCode = 'Error in SignIn annon';
      return null;
    }
  }

  Stream<CustomUser> get user =>
      _firebaseAuth.authStateChanges().map(_userFromFirebaseUser);

  CustomUser get currentUser => _currentUser;

  // Email & Password Sign Up
  Future<String> createUserWithEmailAndPassword(LoginData data) async {
    try {
      print('AUth data' + data.toString());
      await _firebaseAuth
          .createUserWithEmailAndPassword(
            email: data.name,
            password: data.password,
          )
          .then((value) => _currentUser = _userFromFirebaseUser(value.user));
      return null;
    } on FirebaseAuthException catch (exp) {
      switch (exp.code) {
        case "email-already-in-use":
          errorCode = 'Error! Email Already in Use';
          break;
        case "invalid-email":
          errorCode = 'Error! Invalid Email Entered';
          break;
        case "weak-password":
          errorCode = 'Error! Weak Password try with a different one';
          break;
        default:
          errorCode = 'Error! Something Wrong Happened Try again!';
          break;
      }
      return errorCode;
    }
  }

  // Email & Password Sign In
  Future<String> signInWithEmailAndPassword(LoginData data) async {
    try {
      print('AUth data' + data.toString());
      await _firebaseAuth
          .signInWithEmailAndPassword(email: data.name, password: data.password)
          .then((value) => _currentUser = _userFromFirebaseUser(value.user));
      print(_currentUser);
      return null;
    } on FirebaseAuthException catch (exp) {
      switch (exp.code) {
        case 'user-not-found':
          errorCode = 'Error! User  not found';
          break;
        case 'invalid-email':
          errorCode = 'Error! Invalid Email Entered';
          break;
        case 'wrong-password':
          errorCode = 'Error! Wrong Password try with a different one';
          break;
        default:
          errorCode = 'Error! Something Wrong Happened Try again!';
          break;
      }
      return errorCode;
    }
  }

  // Sign Out
  Future signOut() async {
    try {
      return await _firebaseAuth.signOut();
    } catch (e) {
      print("Error in signout");
      print(e.toString());
    }
  }
}