насколько я понимаю, предоставление блока на уровне метода runApp делает его доступным во всем приложении. Я не прав?
У меня есть эта реализация
Мой файл main.dart
void main() => runApp(
RepositoryProvider<AuthenticationRepository>(
create: (context) {
return AuthenticationService();
},
child: MultiBlocProvider(
providers: [
BlocProvider<AuthenticationBloc>(
create: (context) {
final authService =
RepositoryProvider.of<AuthenticationRepository>(context);
return AuthenticationBloc(authService)..add(AppLoaded());
},
),
BlocProvider<SignupBloc>(
create: (context) {
final authService =
RepositoryProvider.of<AuthenticationRepository>(context);
return SignupBloc(authService);
},
)
],
child: MyApp(),
),
),
);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationSuccess) {
return MainScreen(user: state.user);
}
return LoginScreen();
},
),
);
}
}
а потом у меня есть этот виджет:
import 'dart:developer';
import 'package:myapp/features/signup/bloc/signup_bloc.dart';
import 'package:myapp/shared/ui/textStyles.dart';
import 'package:myapp/shared/ui/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
class ProfileInfoForm extends StatefulWidget {
@override
_ProfileInfoFormState createState() => _ProfileInfoFormState();
}
class _ProfileInfoFormState extends State<ProfileInfoForm> {
String _firstname, _lastname, _city, _country, _gender = "";
GlobalKey<FormState> _key = GlobalKey<FormState>();
List<bool> _selections = List.generate(2, (index) => false);
@override
Widget build(BuildContext context) {
final _signupBloc = Provider.of<SignupBloc>(context);
return BlocListener<SignupBloc, SignupState>(
listener: (context, state) {
if (state is CreateAccountFailure) {
log("message");
}
},
child: BlocBuilder(
builder: (context, state) {
if (state is CreateAccountLoading) {
return CircularProgressIndicator();
}
return SingleChildScrollView(
child: Container(
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Form(
key: _key,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 250,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: CustomTextField(
hintText: "Firstname",
secured: false,
onSave: (value) {
setState(() {
_firstname = value;
});
},
onValidate: (value) {
if (value == "") {
return "Please enter your firstname";
}
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: CustomTextField(
hintText: "Lastname",
secured: false,
onSave: (value) {
setState(() {
_lastname = value;
});
},
onValidate: (value) {
if (value == "") {
return "Please enter your lastname";
}
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: Text(
"Select your gender",
style: blackSubTitle,
),
),
ToggleButtons(
fillColor: Colors.white,
renderBorder: true,
color: Colors.black,
selectedBorderColor: Colors.redAccent,
borderRadius: BorderRadius.circular(20),
children: [
Container(
alignment: Alignment.center,
// decoration: BoxDecoration(borderRadius: BorderRadius.circular(20)),
width: ScreenUtil().setWidth(220),
child: Text(
"Female",
style: blackSubTitle,
),
),
Container(
alignment: Alignment.center,
width: ScreenUtil().setWidth(220),
child: Text(
"Male",
style: blackSubTitle,
),
),
],
isSelected: _selections,
onPressed: (index) {
setState(() {
_selections =
List.generate(2, (index) => false);
_selections[index] = !_selections[index];
if (_selections.elementAt(0) == true) {
setState(() {
_gender = "Female";
});
}
if (_selections.elementAt(1) == true) {
setState(() {
_gender = "Male";
});
}
});
},
)
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: FlatButton(
onPressed: () {
if (_key.currentState.validate()) {
_key.currentState.save();
// _signupBloc.add(
// InitProfilePressed(
// firstname: _firstname,
// lastname: _lastname,
// gender: _gender),
// );
} else {}
},
child: Row(
children: [
Expanded(
child: Container(
alignment: Alignment.center,
width: 200,
height: 60,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.black,
),
child: Text(
"Continue",
style: whiteSubTitle,
),
),
),
],
),
),
)
],
),
),
),
),
),
);
},
),
);
}
}
К сожалению, я получаю эту ошибку:
BlocProvider.of() called with a context that does not contain a Bloc of type Bloc<dynamic, dynamic>. No ancestor could be found starting from the context that was passed to BlocProvider.of<Bloc<dynamic, dynamic>>(). This can happen if the context you used comes from a widget above the BlocProvider. The context used was: BlocBuilder<Bloc<dynamic, dynamic>, dynamic>(dirty, state: _BlocBuilderBaseState<Bloc<dynamic, dynamic>,
динамический ›# a73f5 (состояние жизненного цикла: создано))
BlocProvider.of ‹Bloc‹ динамический, динамический ›› (). Это может произойти, если используемый вами контекст исходит из виджета над BlocProvider. Используемый контекст: BlocBuilder ‹Bloc‹ динамический, динамический ›, динамический› (грязный, состояние: _BlocBuilderBaseState ‹Bloc‹ динамический, динамический ›, динамический› # a73f5 (состояние жизненного цикла: создано)) Соответствующий виджет, вызывающий ошибку, был: < br /> BlocListener ‹RegistrationBloc, RegistrationState›
# 23 ComponentElement.performRebuild package: flutter /… / widgets / framework.dart: 4571 # 24 StatefulElement.performRebuild package: flutter /… / widgets / framework.dart: 4719 # 25 Element.rebuild package: flutter /… / widgets / framework .dart: 4262 # 26 BuildOwner.buildScope пакет: flutter /… / widgets / framework.dart: 2667 # 27 WidgetsBinding.drawFrame package: flutter /… / widgets / binding.dart: 866 # 28 RendererBinding._handlePersistentFrameCallback пакет: flutter /… /rendering/binding.dart:286 # 29 Пакет SchedulerBinding._invokeFrameCallback: flutter /… / scheduler / binding.dart: 1115 # 30 Пакет SchedulerBinding.handleDrawFrame: flutter /… / scheduler / binding.dart: 1054 # 31 Пакет SchedulerBinding._handleDrawFrame : flutter /… / scheduler / binding.dart: 970 # 35 _invoke (dart: ui / hooks.dart: 269: 10) # 36 _drawFrame (dart: ui / hooks.dart: 227: 3) (исключено 3 кадра из dart : async)
Что мне не хватает?
спасибо за вашу помощь