My first screen is a login screen and it needs to check if the user is logged in to open the home screen directly but I get an error using this check.
I'm doing the check on initState, the condition is returning true, so looks like the problem is with the Navigator.
What is the correct way to skip the first screen if the user is logged in?
Error:
I/flutter (20803): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (20803): The following assertion was thrown building Navigator-[GlobalObjectKey<NavigatorState>
I/flutter (20803): _WidgetsAppState#8ce27](dirty, state: NavigatorState#db484(tickers: tracking 2 tickers)):
I/flutter (20803): 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 2106 pos 12: '!_debugLocked':
I/flutter (20803): is not true.
I/flutter (20803): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (20803): more information in this error message to help you determine and fix the underlying cause.
Code:
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _emailController = TextEditingController();
final _passController = TextEditingController();
final _formKey = GlobalKey<FormState>();
final _scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
if(FirebaseAuth.instance.currentUser() != null){
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => HomeScreen()
));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: ScopedModelDescendant<UserModel>(
builder: (context, child, model){
if(model.isLoading)
return Center(
child: CircularProgressIndicator(),
);
return Form(
key: _formKey,
child: ListView(
padding: EdgeInsets.all(16),
children: <Widget>[
SizedBox(height: 67),
Icon(Icons.chrome_reader_mode, size: 150, color: Colors.blue,),
SizedBox(height: 16,),
TextFormField(
controller: _emailController,
decoration: InputDecoration(
hintText: "Digite seu e-mail",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
fillColor: Colors.blueAccent
),
keyboardType: TextInputType.emailAddress,
validator: (text){
if(text.isEmpty || !text.contains("@"))
return "E-mail inválido!";
},
),
SizedBox(height: 16,),
TextFormField(
controller: _passController,
decoration: InputDecoration(
hintText: "Digite sua senha",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
fillColor: Colors.blueAccent
),
obscureText: true,
validator: (text){
if(text.isEmpty || text.length < 6)
return "Digite a senha!";
},
),
SizedBox(height: 16,),
FlatButton(
padding: EdgeInsets.all(13),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
),
color: Colors.blue,
child: Text("Entrar",
style: TextStyle(
color: Colors.white,
fontSize: 20
),
),
onPressed: (){
if(_formKey.currentState.validate()){
model.signIn(
email: _emailController.text,
pass: _passController.text,
onSuccess: _onSuccess,
onFail: _onFail,
);
}
},
),
SizedBox(height: 10,),
InkWell(
onTap: (){
if(_emailController.text.isEmpty || !_emailController.text.contains("@")){
_scaffoldKey.currentState.showSnackBar(
SnackBar(content: Text("Insira um e-mail válido para recuperação",
style: TextStyle(fontSize: 14),
),
backgroundColor: Colors.redAccent,
duration: Duration(seconds: 3),
)
);
} else {
model.recoverPass(_emailController.text);
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text("O e-mail de recuperação foi enviado!",
style: TextStyle(fontSize: 14),
),
backgroundColor: Colors.green,
duration: Duration(seconds: 3),
)
);
}
},
child: Text("Esqueci minha senha",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w400
),
textAlign: TextAlign.center,
),
),
SizedBox(height: 30,),
InkWell(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context)=> SignUpScreen())
);
},
child: Text("Não tem conta? Cadastre-se!",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600
),
textAlign: TextAlign.center,
),
),
],
),
);
},
),
);
}
}