When i try to use blocprovider and navigate to a new page i try to use blocprovider.value() but i get a error saying
BlocProvider.of() called with a context that does not contain a Bloc of type CreateTemplateBloc
import 'package:auto_size_text/auto_size_text.dart';
import 'package:dotted_border/dotted_border.dart';
import 'package:easyagreement/bloc/create_template_bloc.dart';
import 'package:easyagreement/views/DefaultPropertyTemplate.dart';
import 'package:easyagreement/views/EditableDefaultTemplate.dart';
import 'package:easyagreement/views/EditableGenericTemplate.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';
import '../sizeConfig.dart';
class SelectOwnTemplate extends StatefulWidget {
SelectOwnTemplate({Key key}) : super(key: key);
@override
_SelectOwnTemplateState createState() => _SelectOwnTemplateState();
}
class _SelectOwnTemplateState extends State
@override
Widget build(BuildContext context) {
SizeConfig().init(context);
double blockHeight = SizeConfig.safeBlockVertical;
double blockWidth = SizeConfig.safeBlockHorizontal;
double margin = blockWidth * 5;
return BlocProvider<CreateTemplateBloc>(
create: (context) => CreateTemplateBloc([]),
lazy: false,
child: SafeArea(
child: Scaffold(
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
//TODO advertisements
height: blockHeight * 6,
margin: EdgeInsets.only(
top: blockHeight * 3, left: margin, right: margin),
color: Colors.red,
),
SizedBox(
height: blockHeight * 2,
),
Container(
height: blockHeight * 10,
// color: Colors.grey,
alignment: Alignment.center,
child: Column(
children: <Widget>[
Container(
height: blockHeight * 5,
// color: Colors.red,
alignment: Alignment.center,
child: AutoSizeText(
"Select Base ",
style: GoogleFonts.montserrat(
fontSize: 30, fontWeight: FontWeight.w400),
minFontSize: 15,
),
),
Container(
height: blockHeight * 5,
// color: Colors.red,
alignment: Alignment.center,
child: AutoSizeText(
"Template",
style: GoogleFonts.montserrat(
fontSize: 30, fontWeight: FontWeight.w400),
minFontSize: 15,
),
),
],
)),
SizedBox(
height: blockHeight * 10,
),
Container(
height: blockHeight * 15,
// color: Colors.red,
margin: EdgeInsets.only(left: margin, right: margin),
alignment: Alignment.topLeft,
child: Row(
children: <Widget>[
Expanded(
child: RaisedButton(
padding: EdgeInsets.all(0),
onPressed: () {
print("default generic template");
// Get.to(EditableGenericTemplate(
// blockHeightFromWidget: blockHeight,
// blockWidthFromWidget: blockWidth,
// ));
},
child: Container(
// color: Colors.greenAccent,
decoration: BoxDecoration(
color: Colors.grey,
border: Border.all(
color: Colors.black, width: blockWidth / 2)),
padding: EdgeInsets.all(blockHeight * 2),
alignment: Alignment.center,
child: Container(
// color: Colors.red,
alignment: Alignment.center,
child: Column(
children: <Widget>[
Expanded(
child: Container(
// color: Colors.grey,
alignment: Alignment.topLeft,
child: AutoSizeText(
"Default",
style: GoogleFonts.montserrat(
fontWeight: FontWeight.w500,
fontSize: 18),
minFontSize: 12,
),
)),
Expanded(
child: Container(
// color: Colors.yellow,
alignment: Alignment.topLeft,
child: AutoSizeText(
"Generic",
style: GoogleFonts.montserrat(
fontWeight: FontWeight.w500,
fontSize: 18),
minFontSize: 12,
),
)),
Expanded(
child: Container(
// color: Colors.grey,
alignment: Alignment.topLeft,
child: AutoSizeText(
"Template",
style: GoogleFonts.montserrat(
fontWeight: FontWeight.w500,
fontSize: 18),
minFontSize: 12,
),
)),
],
)),
),
)),
SizedBox(
width: blockWidth * 5,
),
Expanded(
child: RaisedButton(
padding: EdgeInsets.all(0),
onPressed: () {
print("defult property template");
Navigator.of(context).push(
MaterialPageRoute<CreateTemplateBloc>(
builder: (_) => BlocProvider.value(
value: BlocProvider.of<CreateTemplateBloc>(
context),
child: EditableTemplate(
blockHeightFromWidget: blockHeight,
blockWidthFromWidget: blockWidth,
),
),
),
);
// Get.to(EditableTemplate(
// blockHeightFromWidget: blockHeight,
// blockWidthFromWidget: blockWidth,
// ));
},
child: Container(
decoration: BoxDecoration(
color: Colors.grey,
border: Border.all(
color: Colors.black, width: blockWidth / 2)),
// color: Colors.greenAccent,
padding: EdgeInsets.all(blockHeight * 2),
alignment: Alignment.center,
child: Container(
// color: Colors.red,
alignment: Alignment.center,
child: Column(
children: <Widget>[
Expanded(
child: Container(
// color: Colors.grey,
alignment: Alignment.topLeft,
child: AutoSizeText(
"Default",
style: GoogleFonts.montserrat(
fontWeight: FontWeight.w500,
fontSize: 18),
minFontSize: 12,
),
)),
Expanded(
child: Container(
// color: Colors.yellow,
alignment: Alignment.topLeft,
child: AutoSizeText(
"Property",
style: GoogleFonts.montserrat(
fontWeight: FontWeight.w500,
fontSize: 18),
minFontSize: 12,
),
)),
Expanded(
child: Container(
// color: Colors.grey,
alignment: Alignment.topLeft,
child: AutoSizeText(
"Template",
style: GoogleFonts.montserrat(
fontWeight: FontWeight.w500,
fontSize: 18),
minFontSize: 12,
),
)),
],
)),
),
))
],
),
),
],
),
),
),
),
);
}
}
The formatting on your snippet got messed up somewhere. But the issue is that you are using the context from _SelectOwnTemplateState.build.
I would suggest wrapping your RaisedButton in a Builder, to obtain a new context, which will be able to see the bloc you provided at the top 馃憤
Builder(
builder: (context) => RaisedButton(
onPressed: () {
// Now we can use the BuildContext of our Builder.
final bloc = BlocProvider.of<MyBloc>(context);
}
...
),
)
It worked Thanks a lot
Most helpful comment
It worked Thanks a lot