Reputation: 111
I have a class which wokring with generics: Screen bloc
class ScreenWithUpdater<DataType> extends StatefulWidget {
final Updater<DataType> updater;
final Widget Function(BuildContext, DataType) bodyBuilder;
final Duration updatePeriod;
const ScreenWithUpdater({
Key key,
@required this.updater,
@required this.bodyBuilder,
@required this.updatePeriod,
}) : super(key: key);
@override
_ScreenWithUpdaterState createState() => _ScreenWithUpdaterState();
}
class _ScreenWithUpdaterState<DataType>
extends State<ScreenWithUpdater<DataType>> {
UpdateBloc<DataType> _bloc;
@override
void initState() {
super.initState();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_bloc = UpdateBloc<DataType>(
updatePeriod: widget.updatePeriod,
updater: widget.updater,
);
}
@override
void dispose() {
_bloc.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<UpdateBloc, UpdateState>(
cubit: _bloc,
builder: (
context,
state,
) {
if (state is ShowLoader) {
return LoaderWidget();
} else if (state is UpdateMainState<DataType>) {
return widget.bodyBuilder(context, state.data);
} else {
return Center(
child: Text('Something went wrong'),
);
}
},
);
}
}
And when i'm calling it in the way like this:
ScreenWithUpdater<List<OrderFromApi>>(
updatePeriod: updatePeriod,
updater: OrdersPageUpdater(),
bodyBuilder: (context, orders) => OrdersPage(microOrders: orders),
),
It fails with an exception
type '(BuildContext, List) => OrdersPage' is not a subtype of type '(BuildContext, dynamic) => Widget'
But when i'm calling in the next way
ScreenWithUpdater(
updatePeriod: updatePeriod,
updater: OrdersPageUpdater(),
bodyBuilder: (context, orders) => OrdersPage(microOrders: orders),
),
It works fine
Why? And what should i change to run my first version?
Upvotes: 2
Views: 356
Reputation: 111
An issue was in next line:
@override
_ScreenWithUpdaterState createState() => _ScreenWithUpdaterState();
}
Then i've changed this line to
@override
_ScreenWithUpdaterState createState() => _ScreenWithUpdaterState<DataType>();
}
And everything is working now!
Upvotes: 2