Reputation: 39
I'm getting this error when I'm trying to change the state of the widget by defining a custom function to set state and then I'm calling that function from a new class I created.
setState() called in constructor: _MyHomePageState#da7cc(lifecycle state: created, no widget, not mounted)
HomePage:
import 'package:flutter/material.dart';
import 'package:ed_cell/auth_service.dart';
import 'package:flutter/scheduler.dart';
Widget authStatus = Text('Welcome');
class MyHomePage extends StatefulWidget {
_MyHomePageState myHomePageState= new _MyHomePageState();
@override
_MyHomePageState createState() => myHomePageState;
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return ListView(
children: [
Container(
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(10)),
height: 575,
child: Row(
children: <Widget>[
Column(
children: [
Expanded(
child: Image.asset(
'assets/images/rocket.png',
),
),
],
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
'U create\nWe support',
style:
TextStyle(fontSize: 80, fontWeight: FontWeight.bold),
),
authStatus,
],
),
),
],
),
),
],
);
}
void onAuthChange() {
var x=AuthService().status;
if (AuthService().status == 'Sucessful Sign In') {
setState(() {
authStatus = Text(
'Welcome',
style: TextStyle(color: Colors.grey, fontSize: 50),
);
print(x);
});
print('object'+x);
} else {
setState(() {
authStatus = RaisedButton(
onPressed: null,
color: Colors.deepOrange,
child: Text(
'Join Now',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
);
});
}
}
}
AuthService:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:ed_cell/home_page.dart';
class AuthService{
String status;
signIn(email,password){
FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password).then((user){
print('Signed In '+user.user.uid);
status='Sucessful Sign In';
}).catchError((e){
status='Failure in Sign In';
print(e);
});
MyHomePage().myHomePageState.onAuthChange();
}
}
Upvotes: 1
Views: 1254
Reputation: 17133
The error relates to HomePage
not being inserted into the widget tree yet. You can call setState
from another class as you have already done correctly, but the HomePage
widget has to be somewhere already in the widget tree for it to be able to do setState
.
Upvotes: 1