Necesito ayuda con un bloc que al parecer no está disparando el método fetch con el que llamo a una API.
El error que estoy recibiendo es:
══╡ EXCEPTION CAUGHT BY PROVIDER ╞═══════════════════════════════
The following _CastError was thrown:
type 'Null' is not a subtype of type 'HomeBloc' in type cast
Adjunto código de bloc, métodos de clases, main y widgets.
part 'home_event.dart';
part 'home_state.dart';
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final PlayerService _playerService;
HomeBloc(this._playerService) : super(HomeLoadingState()) {
on<LoadApiEvent>((event, emit) async {
final player = await _playerService.fetchHomeData();
emit(HomeLoadedState(player.playerId, player.playerName,
player.playerPoints, player.playerEmail, player.playerAlbums));
});
}
}
part of 'home_bloc.dart';
abstract class HomeEvent extends Equatable {
const HomeEvent();
}
class LoadApiEvent extends HomeEvent {
@override
List<Object?> get props => [];
}
part of 'home_bloc.dart';
abstract class HomeState extends Equatable {
const HomeState();
}
class HomeLoadingState extends HomeState {
@override
List<Object> get props => [];
}
class HomeLoadedState extends HomeState {
final int playerId;
final String playerName;
final String playerPoints;
final String playerEmail;
final List<PlayerAlbum> playerAlbums;
const HomeLoadedState(this.playerId, this.playerName, this.playerPoints,
this.playerEmail, this.playerAlbums);
@override
List<Object> get props =>
[playerId, playerName, playerPoints, playerEmail, playerAlbums];
}
Adjunto código de main y widget del home, en donde se utiliza el provider:
void main() {
runApp(const MyApp());
}
return MaterialApp(
home: MultiRepositoryProvider(
providers: [
RepositoryProvider(
create: (context) => PlayerService(),
),
],
child: HomePageView(),
));
Este es el widget del home page que dibuja el contenido del bloc:
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => HomeBloc(
RepositoryProvider.of<PlayerService>(context),
)..add(LoadApiEvent()),
child: Scaffold(
backgroundColor: Colors.blue.shade800,
appBar: const CustomAppBar(title: "LaminApp"),
bottomNavigationBar: CircularBottomNavigation(
tabItems,
selectedCallback: (int? selectedPos) {
print("clicked on $selectedPos");
},
selectedPos: selectedPos,
barBackgroundColor: Colors.white,
),
body: SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 30,
),
BlocBuilder<HomeBloc, HomeState>(
builder: (context, state) {
if (state is HomeLoadedState) {
return Text(
state.playerName,
style: CustomTextStyle.cardItemTextStyle,
);
} else {
return Container();
}
},
),