Hi ,
Get this erreur :
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: BlocProvider.of() called with a context that does not contain a Bloc of type AuthenticationBloc. E/flutter (12064): No ancestor could be found starting from the context that was passed to BlocProvider.of
I try to create a little application based in yours flutter_firebase_login.
This error is produced after succesfuil login and switch to AuthenticationAuthenticated state.
when I make reload with rebuild MAJ + R the error go way and I can switch to HomePage
my configuration
cupertino_icons: ^0.1.2
bloc: ^2.0.0
flutter_bloc: ^2.1.1
meta: ^1.1.6
equatable: ^1.0.0
Thank for you help
my main.dart
`void main() {
WidgetsFlutterBinding.ensureInitialized();
BlocSupervisor.delegate = SimpleBlocDelegate();
final UserRepository userRepository = UserRepository();
runApp(
BlocProvider
create: (context) => AuthenticationBloc(
userRepository: userRepository,
)..add(AppStarted()),
child: App(userRepository: userRepository),
),
);
}
class App extends StatelessWidget {
final UserRepository _userRepository;
final ThemeData _theme = Themes.light;
App({Key key, @required UserRepository userRepository})
: assert(userRepository != null),
_userRepository = userRepository,
super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'S1',
theme: _theme,
home: BlocBuilder
builder: (context, state) {
BlocProvider.of
if (state is AuthenticationUnauthenticated) {
return LoginPage(userRepository: _userRepository);
}
if (state is AuthenticationAuthenticated) {
return HomePage(name: "Test");
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
return SplashPage();
},
),
);
}
}`
Hi @vassilux 👋
Thanks for opening an issue!
Are you able to share a link to the full source code? Thanks!
thank for you response
this is a link to repo https://github.com/vassilux/fsone/tree/master/lib
No problem! I took a quick look and don't understand why you need this line https://github.com/vassilux/fsone/blob/fefc5da5cccbc6ec1283c4927fd85432746cf73a/lib/main.dart#L54. You can't access the bloc from the same context in which it was provided.
yes it was I just stupid teste I forgot to delete
Felix,
my problem is in login_form.dart
if (state.isSuccess) {
BlocProvider.of<AuthenticationBloc>(context)
.add(LoggedIn());
}
there is a chance that I don't understand well something with flutter_bloc library , so LoginPage does not contain a AuthentificaitonBloc
I found some examples on the `https://www.didierboelens.com/fr/2018/12/reactive-programming---streams---bloc---cas-practiques-dutilisation/
void main() => runApp(Application());
class Application extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider
bloc: AuthenticationBloc(),
child: MaterialApp(
title: 'BLoC Samples',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: InitializationPage(),
),
);
}
}`
Other thing I can not see the property bloc of BlocProvider
Hi @vassilux I took a look at https://github.com/vassilux/fsone/tree/master and you are missing required files such as a pubspec.yaml so I am unable to run the project locally. Can you please update the project so that it can be run locally? Thanks!
yes done
@vassilux I ran it locally and had no issues. My login_form.dart looks like:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fsone/blocs/authentication/bloc.dart';
import 'package:fsone/blocs/login/bloc.dart';
import 'package:fsone/config/assets.dart';
import 'package:fsone/widgets/headers/wavy_header.dart';
import 'login_button.dart';
class LoginForm extends StatefulWidget {
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
//LoginBloc _loginBloc;
bool get isPopulated =>
_usernameController.text.isNotEmpty &&
_passwordController.text.isNotEmpty;
bool isLoginButtonEnabled(LoginState state) {
return state.isFormValid && isPopulated && !state.isSubmitting;
}
@override
void initState() {
super.initState();
_usernameController.addListener(_onEmailChanged);
_passwordController.addListener(_onPasswordChanged);
}
void _onEmailChanged() {
BlocProvider.of<LoginBloc>(context).add(
UserNameChanged(username: _usernameController.text),
);
}
void _onPasswordChanged() {
BlocProvider.of<LoginBloc>(context).add(
PasswordChanged(password: _passwordController.text),
);
}
@override
Widget build(BuildContext context) {
void _onFormSubmitted() {
BlocProvider.of<LoginBloc>(context).add(
LoginButtonPressed(
username: _usernameController.text,
password: _passwordController.text,
),
);
}
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
if (state.isFailure) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text('Login Failure'), Icon(Icons.error)],
),
backgroundColor: Colors.red,
),
);
}
if (state.isSubmitting) {
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Logging In...'),
CircularProgressIndicator(),
],
),
),
);
}
if (state.isSuccess) {
BlocProvider.of<AuthenticationBloc>(context).add(
LoggedIn(token: 'token'),
);
}
},
child: BlocBuilder<LoginBloc, LoginState>(
builder: (context, state) {
return Padding(
padding: EdgeInsets.all(0.2),
child: Form(
child: ListView(
children: <Widget>[
buildHeaderSectionWidget(),
/*Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: Image.asset('assets/flutter_logo.png', height: 50),
),*/
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey.withOpacity(0.5),
width: 1.0,
),
borderRadius: BorderRadius.circular(20.0),
),
margin: const EdgeInsets.symmetric(
vertical: 10.0, horizontal: 20.0),
child: Row(
children: <Widget>[
new Padding(
padding: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 15.0),
child: Icon(
Icons.person_outline,
color: Colors.grey,
),
),
Container(
height: 30.0,
width: 1.0,
color: Colors.grey.withOpacity(0.5),
margin:
const EdgeInsets.only(left: 00.0, right: 10.0),
),
new Expanded(
child: TextFormField(
controller: _usernameController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter your username',
hintStyle: TextStyle(color: Colors.grey),
),
keyboardType: TextInputType.text,
autovalidate: true,
autocorrect: false,
validator: (_) {
return !state.isUsernameValid
? 'Invalid Username'
: null;
}),
)
],
),
),
//fin
Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey.withOpacity(0.5),
width: 1.0,
),
borderRadius: BorderRadius.circular(20.0),
),
margin: const EdgeInsets.symmetric(
vertical: 10.0, horizontal: 20.0),
child: Row(
children: <Widget>[
new Padding(
padding: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 15.0),
child: Icon(
Icons.lock_open,
color: Colors.grey,
),
),
Container(
height: 30.0,
width: 1.0,
color: Colors.grey.withOpacity(0.5),
margin:
const EdgeInsets.only(left: 00.0, right: 10.0),
),
new Expanded(
child: TextFormField(
controller: _passwordController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'Enter your password',
hintStyle: TextStyle(color: Colors.grey),
),
obscureText: true,
autovalidate: true,
autocorrect: false,
validator: (_) {
return !state.isPasswordValid
? 'Invalid Password'
: null;
},
),
)
],
),
),
// end new passwpord
Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
LoginButton(
onPressed: isLoginButtonEnabled(state)
? _onFormSubmitted
: null,
),
//GoogleLoginButton(),
//CreateAccountButton(userRepository: _userRepository),
],
),
),
],
),
),
);
},
),
);
}
buildHeaderSectionWidget() {
return Column(children: <Widget>[
WavyHeader(),
// Container(
// margin: EdgeInsets.only(top: 10),
// child: Image.asset(Assets.app_icon, height: 100)),
Container(
margin: EdgeInsets.only(top: 10),
child: Text('S1',
style: TextStyle(
// color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 22)))
]);
}
@override
void dispose() {
_usernameController.dispose();
_passwordController.dispose();
super.dispose();
}
}
Closing for now since it seems like everything is working as expected. Feel free to comment with additional information/questions and I'm happy to continue the conversation 👍
Ok Felix,
It is interesting point to understand where is problem , maybe this behavior is caused by environment
Application was tested on debug mode debug on device and Emulator
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel master, v1.13.6-pre.16, on Microsoft Windows [version 10.0.18362.207], locale fr-FR)
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[√] Android Studio (version 3.5)
[√] VS Code, 64-bit edition (version 1.38.1)
[√] Connected device (1 available)
• No issues found!
@vassilux do you have any problems running the default login example?
Yes Felix it works good I just added
android.useAndroidX=true
android.enableJetifier=true
and changed compileSdkVersion 28
I will continue to look for solution and copare with your example
Felix,
so I used your default login example as base of my project and now it is work , my code was imported into your project
Thank for your help and happy new year
Most helpful comment
Felix,
so I used your default login example as base of my project and now it is work , my code was imported into your project
Thank for your help and happy new year