Reputation: 351
I'm trying to use the blocks but I don't understand. When I want to group my navigation in a BlocRouter class I get an error message. The error message is :
"The argument type 'BlocProvider' can't be assigned to the parameter type 'Route'."
Here my files :
MainScaffold : In this file I have the error message
class MainScaffold extends StatelessWidget{
final String title;
MainScaffold({this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body : Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text("Choose a bloc"),
RaisedButton(
child: Text("Counter"),
onPressed : () => Navigator.of(context).push(BlocRouter().counterDetail()), // ERROR MESSAGE HERE
),
],
),
),
);
}
}
BlocRouter :
class BlocRouter{
MaterialPageRoute counterPage() => MaterialPageRoute(builder: (context) => counterDetail());
BlocProvider counterDetail() => BlocProvider<CounterBloc>(bloc: CounterBloc(),child: CounterScreen());
}
CounterBloc :
class CounterBloc extends Bloc{
int counter = 0;
final _streamController = StreamController<int>();
Sink<int> get sink =>_streamController.sink;
Stream<int> get stream => _streamController.stream;
CounterBloc(){
sink.add(counter);
}
incrementCounter(){
counter = counter + 1;
sink.add(counter);
}
decrementCounter(){
counter = counter - 1;
sink.add(counter);
}
@override
dispose(){
return _streamController.close();
}
}
BlocProvider :
class BlocProvider<T extends Bloc> extends StatefulWidget{
final T bloc;
final Widget child;
BlocProvider({@required this.bloc, @required this.child});
static T of<T extends Bloc>(BuildContext context){
final BlocProvider<T> provider = context.findAncestorWidgetOfExactType<BlocProvider<T>>();
return provider.bloc;
}
State createState() => _BlocProviderState();
}
class _BlocProviderState extends State<BlocProvider>{
@override
Widget build(BuildContext context) {
return widget.child;
}
@override
void dispose() {
widget.bloc.dispose();
super.dispose();
}
}
Upvotes: 0
Views: 908
Reputation: 5746
push
requires a Route
and you're passing a BlocProvider
. Pass a MaterialPageRoute
in push
and do the following:
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => BlocRouter().counterDetail()),
);
Upvotes: 1