I used Flutter BloC to write down my LogIn web page.
I adopted the tutorial on YouTube step-by-step (and proceed the code my classmate began.)
The error:
“exception”:”Dangerous state: Tried to learn a supplier that threw through the creation
of its worth.nThe exception occurred through the creation of sort
LoginBloc.
principal.dart:
void principal() {
runApp(DevicePreview(
enabled: !kReleaseMode,
builder: (context) => MultiProvider(
suppliers: [
ChangeNotifierProvider(
create: (_) => ThemeProvider(),
),
ChangeNotifierProvider(
create: (_) => OCRImageProvider(),
),
BlocProvider(
create: (context) => PageBloc(),
),
BlocProvider(
create: (context) => JWTBloc(),
),
BlocProvider(
create: (context) => FileBloc(),
),
BlocProvider(
create: (context) => FilesBloc(),
),
BlocProvider(
create: (context) => ProfileBloc(),
),
BlocProvider(
create: (context) => AccessMethodBloc(),
),
BlocProvider(
create: (context) => ArticleBloc(),
),
BlocProvider(
create: (context) => PasswordBloc(),
),
BlocProvider(
create: (context) => LoginBloc(authRepo: AuthRepository()),
),
RepositoryProvider(
create: (context) => LoginBloc(authRepo: AuthRepository()),
child: SignInPage(),
),
],
little one: const MyApp(),
)));
}
class MyApp extends StatelessWidget {
const MyApp({tremendous.key});
@override
Widget construct(BuildContext context) {
remaining themeProvider = Supplier.of<ThemeProvider>(context);
return RepositoryProvider(
create: (context) => AuthRepository(),
little one: MaterialApp(
locale: DevicePreview.locale(context),
builder: DevicePreview.appBuilder,
title: 'Examine-Savvy',
theme: LightStyle.theme,
darkTheme: DarkStyle.theme,
themeMode: themeProvider.themeMode,
onGenerateRoute: RouteGenerator.generateRoute,
debugShowCheckedModeBanner: false,
dwelling: Container(
alignment: Alignment.heart,
ornament: const BoxDecoration(
picture: DecorationImage(
picture: AssetImage('property/photographs/preliminary.jpg'),
match: BoxFit.cowl,
)),
little one: const HomePage(),
),
),
);
}
}
sign_in.dart:
class SignInPage extends StatefulWidget {
const SignInPage({tremendous.key});
@override
State<SignInPage> createState() => _SignInPageState();
}
class _SignInPageState extends State<SignInPage> {
remaining _formKey = GlobalKey<FormState>();
@override
Widget construct(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).primaryColor,
physique: RepositoryProvider(
create: (context) => LoginBloc(
authRepo: context.learn<AuthRepository>(),
),
little one:
SafeArea(
little one: Column(youngsters: [
//...
Expanded(
flex: 11,
child: _LoginForm(),
),
//...
),
)
);
}
Widget _LoginForm() {
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
final formStatus = state.formStatus;
if (formStatus is SubmissionFailed) {
_showSnackBar(context, formStatus.exception.toString());
}
},
child: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 40),
child: Column(
//mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
height: 110,
),
_EmailField(),
const SizedBox(
height: 25,
),
_PasswordField(),
const SizedBox(
height: 210,
),
_SignInButton(),
],
),
),
));
}
Widget _EmailField() {
return BlocBuilder<LoginBloc, LoginState>(builder: (context, state) {
return TextFormField(
model: TextStyle(coloration: Theme.of(context).textTheme.titleMedium!.coloration),
ornament: InputDecoration(
hintText: 'E mail',
hintStyle:
TextStyle(coloration: Theme.of(context).textTheme.titleMedium!.coloration),
stuffed: true,
fillColor: Theme.of(context).inputDecorationTheme.fillColor,
),
validator: (worth) => state.isValidEmail ? null : 'Invalid e-mail handle. Please strive once more.',
onChanged: (worth) => context.learn<LoginBloc>().add(
LoginEmailChanged(e-mail: worth),
),
);
});
}
Widget _PasswordField() {
return BlocBuilder<LoginBloc, LoginState>(builder: (context, state) {
return TextFormField(
obscureText: true,
model: TextStyle(coloration: Theme.of(context).textTheme.titleMedium!.coloration),
ornament: InputDecoration(
hintText: 'Password',
hintStyle:
TextStyle(coloration: Theme.of(context).textTheme.titleMedium!.coloration),
stuffed: true,
fillColor: Theme.of(context).inputDecorationTheme.fillColor,
),
validator: (worth) => state.isValidPassword ? null : 'Password should be no less than 8 characters lengthy.',
onChanged: (worth) => context.learn<LoginBloc>().add(
LoginPasswordChanged(password: worth),
),
);
});
}
Widget _SignInButton() {
return BlocBuilder<LoginBloc, LoginState>(builder: (context, state) {
return state.formStatus is FormSubmitting
? CircularProgressIndicator()
: SizedBox(
width: 189,
peak: 49,
little one: ElevatedButton(
model: Theme.of(context).elevatedButtonTheme.model,
onPressed: () {
debugPrint('Click on "sign up!" button');
if (_formKey.currentState!.validate()) {
context.learn<LoginBloc>().add(LoginSubmitted());
}
},
little one: const Textual content(
'Check in',
textAlign: TextAlign.heart,
model: TextStyle(
coloration: Colours.white,
fontSize: 23,
fontFamily: 'Play',
fontWeight: FontWeight.daring),
),
),
);
});
}
}
login_bloc.dart:
class LoginBloc extends Bloc<LoginEvent, LoginState> {
remaining AuthRepository authRepo;
LoginBloc({required this.authRepo})
: tremendous(LoginState()) {
on<LoginEvent>(_onEvent);
}
Future<void> _onEvent(LoginEvent occasion, Emitter<LoginState> emit) async {
if (occasion is LoginEmailChanged) {
emit(state.copyWith(e-mail: occasion.e-mail));
}
// password replace
else if (occasion is LoginPasswordChanged) {
emit(state.copyWith(password: occasion.password));
}
//type submitted
else if (occasion is LoginSubmitted) {
emit(state.copyWith(formStatus: FormSubmitting()));
strive {
await authRepo.login();
emit(state.copyWith(formStatus: SubmissionSuccess()));
} catch (e) {
emit(state.copyWith(formStatus: SubmissionFailed(e.toString() as Exception)));
}
}
}
}
It really works earlier than however now the code wasn’t working, and I can not discover the rationale.:(
btw. the final time it work, the TextStyle(coloration: Theme.of(context).textTheme.titleMedium!.coloration) did not change the textual content coloration!