flutter – I get the error The following _CastError was thrown building _BodyBuilder: type ‘Null’ is not a subtype of type ‘JokeBloc’ in type cast

Here is the My App Screen:

import 'package:api_call_sample/Repository/JokeRepository.dart';
import 'package:api_call_sample/bloc/jokeevent.dart';
import 'package:api_call_sample/bloc/jokestate.dart';
import 'package:flutter/material.dart';
import "package:api_call_sample/bloc/jokebloc.dart";
import 'package:flutter_bloc/flutter_bloc.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocProvider(
          create: (context) => JokeBloc(
                RepositoryProvider.of<JokeRepository>(context),
              )..add(LoadJokeEvent()),
          child: const HomeScreen()),
    );
  }
}

Here is the HomeScreen Screen :Getting Error in the Bloc Builder screen

        class HomeScreen extends StatefulWidget {
          const HomeScreen({Key? key}) : super(key: key);
        
          @override
          State<HomeScreen> createState() => _HomeScreenState();
        }
        
    
        class _HomeScreenState extends State<HomeScreen> {
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: const Text("Joke Api"),
              ),
              **body: Column( children: [**
                **BlocBuilder<JokeBloc, JokeState>(builder: (context, state) {**
                  print(state);
                  if (state is JokeLoadingState) {
                    print("Loading state");
                    return const CircularProgressIndicator();
                  }
                  if (state is JokeLoadedState) {
                    print(state.jokeData.toString());
                    return ExpansionTile(
                      title: Text(state.jokeData.setup),
                      children: [
                        Container(
                            margin: const EdgeInsets.all(5.0),
                            child: Text(state.jokeData.delivery))
                      ],
                    );
                  }
                  if (state is JokeErrorState) {
                    print("Error state");
                    return Text(state.error.toString());
                  }
                  return Container();
                }),
                ElevatedButton(
                    onPressed: () {
                      BlocProvider.of<JokeBloc>(context).add(LoadJokeEvent());
                    },
                    child: const Text('Load More'))
              ]),
            );
          }
        }
    

Here is the JokeBloc:

    import 'package:bloc/bloc.dart';
    import 'jokestate.dart';
    import 'jokeevent.dart';
    import 'package:api_call_sample/Repository/JokeRepository.dart';
    
    
    
    class JokeBloc extends Bloc<JokeEvent,JokeState>{
      
     final JokeRepository _jokeRepository;
    
      JokeBloc(this._jokeRepository) : super(JokeLoadingState())
       {
        on<LoadJokeEvent>((event, emit) async {
          emit(JokeLoadingState());
          try {
            final joke = await _jokeRepository.getJoke();
            emit(JokeLoadedState(joke));
          } catch (e) {
            emit(JokeErrorState(e.toString()));
          }
        });
      }
    }

Error Message : The following _CastError was thrown building _BodyBuilder: type ‘Null’ is not a subtype of type ‘JokeBloc’ in type cast.

The relevant error-causing widget was:Scaffold Scaffold:file:///Applications/flutter/api_call_sample/lib/main.dart:37:12

The relevant error-causing widget was: Scaffold Scaffold:file:///Applications/flutter/api_call_sample/lib/main.dart:37:12

When the exception was thrown, this was the stack: #0 _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:786:72) #1 _CreateInheritedProviderState.debugFillProperties (package:provider/src/src/inherited:786:72) ) #2 _InheritedProviderScopeElement.debugFillProperties (package:provider/src/inherited_provider.dart:631:20) #3 DiagnosticableNode.builder. (package:flutter/src/foundation/diagnostics.dart:2992:17) #4 DiagnosticableNode.builder (package:flutter/src/foundation/diagnostics.dart:2995:8) #5 DiagnosticableNode.getProperties (package:flutter/src) /foundation/diagnostics.dart:3009:105) #6 TextTreeRenderer._debugRender (package:flutter/src/foundation/diagnostics.dart:1244:63) #7 TextTreeRenderer.render (package:flutter/src/foundation/diagnostics.dart) :1121:14) #8 DiagnosticsNode.toStringDeep. (package:flutter/src/foundation/diagnostics.dart:1776:9) #9 DiagnosticsNode.toStringDeep (package:flutter/src/foundation/diagnostics.dart:1783:6) #10 DiagnosticsNode.toString. (package:flutter/src/foundation/diagnostics.dart:1694:18) #11 DiagnosticsNode.toString (package:flutter/src/foundation/diagnostics.dart:1706:6) #12 Diagnosticable.toString. (package:flutter/src/foundation/diagnostics.dart:3168:78) #13 Diagnosticable.toString (package:flutter/src/foundation/diagnostics.dart:3170:6) #14 _StringBase._interpolate (dart:core-patch /string_patch.dart:853:19) #15 RepositoryProvider.of (package:flutter_bloc/src/repository_provider.dart:78:12) #16 MyApp.build. (package:api_call_sample/main.dart:20:36) #17 _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:736:36) #18 _InheritedProviderScoperElement.value (package:provider.itedart_provin5:5 :33) #19 Provider.of (package:provider/src/provider.dart:303:37) #20 ReadContext.read (package:provider/src/provider.dart:649:21) #21 _BlocBuilderBaseState.initState (package) :flutter_bloc/src/bloc_builder.dart:130:36) #22 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4942:57) #23 ComponentElement.mount (package:flutter/src/widgets/framework) .dart:4781:5) #24 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16) #25 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6350:36) ) #26 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6362:32) … Normal element mounting (19 frames) #45 Element.inflateWidget (package:flutter/src/widgets/framework.dart) :3817:16) #46 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6350:36) #47 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6362:32) … Normal mounting element (99 frames) #146 _InheritedProviderScopeElement.mount (package:provider/src/inherited_provider.dart:411:11) … Normal element mounting (7 frames) #153 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222: 11) … Normal element mounting (7 frames) #160 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11) … Normal element mounting (275 frames) #435 Element.inflateWidget (package:flutter/ src/widgets/framework.dart:3817:16) #436 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6350:36) #437 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework. dart:6362:32) … Normal element mounting (377 frames) #814 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3817:16) #815 Element.updateChild (package:flutter/src/widgets/framework.dart:3551:18) #816 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1202:16) #817 RenderObjectToWidgetElement.mount ( package:flutter/src/widgets/binding.dart:1171:5) #818 RenderObjectToWidgetAdapter.attachToRenderTree. (package:flutter/src/widgets/binding.dart:1119:18) #819 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2597:19) #820 RenderObjectToWidgetAdapter.attachrcToRenderTree (package:flutter/s) /widgets/binding.dart:1118:13) #821 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:953:7) #822 WidgetsBinding.scheduleAttachRootWidget. (package:flutter/src/widgets/binding.dart:933:7) (elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

═︎ ═︎

Another exception was thrown: Each child must be laid out exactly once.

Leave a Comment