Hello,
I'm new to Flutter and i have a problem with you lib,
When i update my state the state does not update like intented.
this is my bloc ,
import 'package:bloc/bloc.dart';
import 'package:smartwork/src/events/Auth.event.dart';
import 'package:smartwork/src/repositories/Auth.repo.dart';
import 'package:smartwork/src/states/Auth.state.dart';
class AuthBloc extends Bloc<AuthEvent, AuthState> {
void authRequest(email, password) {
dispatch(AuthRequest(email, password));
}
@override
AuthState get initialState => AuthState.initial();
@override
Stream<AuthState> mapEventToState(AuthEvent event) async* {
if (event is AuthRequest) {
var data;
try {
yield AuthState(isFetching: true);
data = await AuthRepo().authRequest(event.email, event.password);
yield AuthState(token: data["token"], auth: data["auth"]);
} catch (err) {
print(err);
//yield AuthState(error: err.toString());
} finally {
yield AuthState(isFetching: false);
}
}
}
}
This is my initial state,
class AuthState {
final String token;
final bool isFetching;
final String error;
final bool auth;
const AuthState({this.token, this.isFetching, this.error, this.auth});
factory AuthState.initial() =>
AuthState(token: "", auth: false, isFetching: false);
}
I set my new state with :
yield AuthState(isFetching: true);
I know it is not really a bug report but i don't find any documentation that show how i update my state like that.
Thank you for hour help,
Michel
Hi, your bloc seems okay.
If you don't get state changes maybe your subscription has some issues. Can you paste the relvant part of your widget?
A guess: did you try with debugger? Maybe it's just too fast?
yield AuthState(isFetching: true);
data = await AuthRepo().authRequest(event.email, event.password);
yield AuthState(token: data["token"], auth: data["auth"]);
I usually run into this problem when an Event
or State
class fails to properly call the Equatable super constructor. You might confirm that for the relevant classes before further debugging. :smile:
````
import "package:flutter/material.dart";
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:smartwork/src/blocs/Auth.bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:smartwork/src/components/form/LoginForm.dart';
import 'package:smartwork/src/pages/Loading.dart';
import 'package:smartwork/src/states/Auth.state.dart';
class SignInPage extends StatefulWidget {
@override
_SignInPageState createState() => new _SignInPageState();
}
class _SignInPageState extends State
final _authBloc = AuthBloc();
@override
Widget build(BuildContext context) {
return new Scaffold(
body: BlocBuilder(
bloc: AuthBloc(),
builder: (BuildContext context, AuthState state) {
if (state.isFetching == true) {
return LoadingPage();
}
if (state.auth == false && state.isFetching == false) {
return Center(
child: ListView(
shrinkWrap: true,
children:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
new Container(
child: Text(
"Sign In",
style: TextStyle(fontSize: 40, color: Colors.black54),
),
),
new Container(
padding:
(EdgeInsets.only(top: 50, left: 35, right: 35)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
new LoginForm(),
])),
new Container(
padding: EdgeInsets.only(top: 45),
child: Column(
children:
new Text(
"Or login with",
style: TextStyle(
color: Colors.black38,
),
),
],
),
),
]),
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children:
Container(
padding: EdgeInsets.only(top: 20),
child: Row(
children:
new IconButton(
icon: Icon(FontAwesomeIcons.facebookF),
color: Colors.black54,
iconSize: 30,
onPressed: () {},
),
new IconButton(
icon: Icon(FontAwesomeIcons.google),
color: Colors.black54,
iconSize: 30,
onPressed: () {},
),
],
),
)
],
),
new Column(
children:
Container(
padding: EdgeInsets.only(top: 200),
child: new Column(
children:
FlatButton(
onPressed: () {
Navigator.of(context).pushNamed("/SignUp");
},
child: Text(
"Sign Up",
style:
TextStyle(fontSize: 18, color: Colors.blue),
)),
FlatButton(
onPressed: () {},
child: Text(
"Forgot Password ? ",
style:
TextStyle(fontSize: 13, color: Colors.black38),
),
),
],
),
),
],
),
],
));
}
},
));
}
@override
void dispose() {
_authBloc.dispose();
super.dispose();
}
}
````
Here when the isFetching state go to true it suppose to show me the Loading page but it do nothing
Hi @MichelMelhem 馃憢
Thanks for opening an issue and thanks for everyone who helped!
Regarding your question, it seems you have multiple AuthBloc instances. You should make sure that the bloc that you give to BlocBuilder is the same one you鈥檙e dispatching events to. In your example you鈥檙e passing a brand new AuthBloc instance to BlocBuilder instead of the instance that you鈥檝e already created (_authBloc).
Hope that helps! 馃憤
Thank you for you help !
And continue to dev this amazing lib 馃憤 @felangel
Most helpful comment
Thank you for you help !
And continue to dev this amazing lib 馃憤 @felangel