Bloc: BlocProvider.of() called with a context that does not contain a Bloc of type AuthenticationBloc.

Created on 9 Feb 2020  ยท  11Comments  ยท  Source: felangel/bloc

I have a problem with AuthenticationBloc and I'm not sure how can I fix it.

Here's the main.dart:

import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:new_app2020/model/user/user.dart';
import 'package:new_app2020/ui/screens/splash_screen.dart';
import 'package:new_app2020/ui/screens/home_screen.dart';
import 'package:new_app2020/ui/screens/login_screen.dart';
import 'package:new_app2020/authentication/authentication.dart';
import 'package:new_app2020/login/loading_indicator.dart';

import 'package:new_app2020/ui/theme.dart';

class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() {
  BlocSupervisor.delegate = SimpleBlocDelegate();
  final user = User();
  runApp(
    BlocProvider<AuthenticationBloc>(
      create: (context) {
        return AuthenticationBloc(user: user)..add(AppStarted());
      },
      child: MyApp(user: user),
    ),
  );
}

class MyApp extends StatelessWidget {
  final User user;

  MyApp({Key key, @required this.user}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: buildTheme(),
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
          builder: (context, state) {
        if (state is AuthenticationAuthenticated) {
          return HomeScreen();
        }
        if (state is AuthenticationUnauthenticated) {
          return LoginScreen(user: user);
        }
        if (state is AuthenticationLoading) {
          return LoadingIndicator();
        }
        return SplashScreen();
      }),

    );
  }
}

The error is:

flutter: โ•โ•โ•ก EXCEPTION CAUGHT BY WIDGETS LIBRARY โ•žโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
flutter: The following assertion was thrown building Builder:
flutter:         BlocProvider.of() called with a context that does not contain a Bloc of type
flutter: AuthenticationBloc.
flutter:         No ancestor could be found starting from the context that was passed to
flutter: BlocProvider.of<AuthenticationBloc>().
flutter:
flutter:         This can happen if the context you used comes from a widget above the BlocProvider.
flutter:
flutter:         The context used was: BlocBuilder<AuthenticationBloc, AuthenticationState>(dirty, state:
flutter: _BlocBuilderBaseState<AuthenticationBloc, AuthenticationState>#43def(lifecycle state: created))
flutter:
flutter:
flutter: The relevant error-causing widget was:
flutter:   MaterialApp file:///Users/user/Desktop/FYP/new_app2020/lib/app.dart:55:12

Thank you!

question

Most helpful comment

Thanks so much @felangel.. it was that.. now everything is working.. thanks for your time and help!

All 11 comments

Found the problem and fixed

How did you fixed the probllem @dannis3434 ?? thank you..

How did you fixed the probllem @dannis3434 ?? thank you..

I just rebuild the project again then everything works fine. Do u have similar problem?

Yes, currently i have the same problem.. and i stop the app and run it again and the problem continues..

This is my code:

```import 'package:flutter/material.dart';

import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:otunus/src/authentication/application/service/authentication_app_service.dart';
import 'package:otunus/src/authentication/infrastructure/repository/authentication_repository.dart';

import 'package:otunus/src/authentication/domain/bloc/authentication.dart';
import 'package:otunus/src/login/presentation/widget/loading_indicator.dart';
import 'package:otunus/src/shared/splash/presentation/screen/splash_screen.dart';
import 'package:otunus/src/home/presentation/screen/home_screen.dart';
import 'package:otunus/src/login/presentation/screen/login_screen.dart';

class SimpleBlocDelegate extends BlocDelegate {
@override
void onEvent(Bloc bloc, Object event) {
super.onEvent(bloc, event);
print(event);
}

@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
print(transition);
}

@override
void onError(Bloc bloc, Object error, StackTrace stacktrace) {
super.onError(bloc, error, stacktrace);
print(error);
}
}

void main() {
BlocSupervisor.delegate = SimpleBlocDelegate();
final authenticationRepository = AuthenticationRepository();
final authenticationAppService = AuthenticationAppService();
runApp(
BlocProvider(
create: (context) {
return AuthenticationBloc(authenticationAppService: authenticationAppService)
..add(AppStarted());
},
child: App(authenticationRepository: authenticationRepository),
),
);
}

class App extends StatelessWidget {
final AuthenticationRepository authenticationRepository;

App({Key key, @required this.authenticationRepository}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocBuilder(
builder: (context, state) {
if (state is AuthenticationAuthenticated) {
return HomeScreen();
}
if (state is AuthenticationUnauthenticated) {
return LoginScreen(authenticationRepository: authenticationRepository);
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
return SplashScreen();
},
),
);
}
}
```

And the problem says:

This can happen if the context you used comes from a widget above the BlocProvider.

The context used was: BlocBuilder(dirty, state: _BlocBuilderBaseState#c9263(lifecycle state: created))

The relevant error-causing widget was
MaterialApp lib/src/app.dart:57

The line 57 is the return MaterialApp(..) which have the BlocBuilder inside.

So i don't know what is happening :S

This is my code:


import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:otunus/src/authentication/application/service/authentication_app_service.dart';
import 'package:otunus/src/authentication/infrastructure/repository/authentication_repository.dart';

import 'package:otunus/src/authentication/domain/bloc/authentication.dart';
import 'package:otunus/src/login/presentation/widget/loading_indicator.dart';
import 'package:otunus/src/shared/splash/presentation/screen/splash_screen.dart';
import 'package:otunus/src/home/presentation/screen/home_screen.dart';
import 'package:otunus/src/login/presentation/screen/login_screen.dart';


class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() {
  BlocSupervisor.delegate = SimpleBlocDelegate();
  final authenticationRepository = AuthenticationRepository();
  final authenticationAppService = AuthenticationAppService();
  runApp(
    BlocProvider<AuthenticationBloc>(
      create: (context) {
        return AuthenticationBloc(authenticationAppService: authenticationAppService)
          ..add(AppStarted());
      },
      child: App(authenticationRepository: authenticationRepository),
    ),
  );
}

class App extends StatelessWidget {
  final AuthenticationRepository authenticationRepository;

  App({Key key, @required this.authenticationRepository}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          if (state is AuthenticationAuthenticated) {
            return HomeScreen();
          }
          if (state is AuthenticationUnauthenticated) {
            return LoginScreen(authenticationRepository: authenticationRepository);
          }
          if (state is AuthenticationLoading) {
            return LoadingIndicator();
          }
          return SplashScreen();
        },
      ),
    );
  }
}

And the problem says:

This can happen if the context you used comes from a widget above the BlocProvider.

The context used was: BlocBuilder(dirty, state: _BlocBuilderBaseState#c9263(lifecycle state: created))

The relevant error-causing widget was
MaterialApp lib/src/app.dart:57

The line 57 is the return MaterialApp(..) which have the BlocBuilder inside.

So i don't know what is happening :S

I added the bloc provider. You can try this.

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: buildTheme(),
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
          bloc: BlocProvider.of<AuthenticationBloc>(context),
          builder: (context, state) {
            if (state is AuthenticationAuthenticated) {
              return HomeScreen();
            }
            if (state is AuthenticationUnauthenticated) {
              return LoginScreen(user: user);
            }
            if (state is AuthenticationLoading) {
              return LoadingIndicator();
            }
            return SplashScreen();
          }),

    );
  }

@fjugaldev can you share a link to your app?

This is my code:


import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:otunus/src/authentication/application/service/authentication_app_service.dart';
import 'package:otunus/src/authentication/infrastructure/repository/authentication_repository.dart';

import 'package:otunus/src/authentication/domain/bloc/authentication.dart';
import 'package:otunus/src/login/presentation/widget/loading_indicator.dart';
import 'package:otunus/src/shared/splash/presentation/screen/splash_screen.dart';
import 'package:otunus/src/home/presentation/screen/home_screen.dart';
import 'package:otunus/src/login/presentation/screen/login_screen.dart';


class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() {
  BlocSupervisor.delegate = SimpleBlocDelegate();
  final authenticationRepository = AuthenticationRepository();
  final authenticationAppService = AuthenticationAppService();
  runApp(
    BlocProvider<AuthenticationBloc>(
      create: (context) {
        return AuthenticationBloc(authenticationAppService: authenticationAppService)
          ..add(AppStarted());
      },
      child: App(authenticationRepository: authenticationRepository),
    ),
  );
}

class App extends StatelessWidget {
  final AuthenticationRepository authenticationRepository;

  App({Key key, @required this.authenticationRepository}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          if (state is AuthenticationAuthenticated) {
            return HomeScreen();
          }
          if (state is AuthenticationUnauthenticated) {
            return LoginScreen(authenticationRepository: authenticationRepository);
          }
          if (state is AuthenticationLoading) {
            return LoadingIndicator();
          }
          return SplashScreen();
        },
      ),
    );
  }
}

And the problem says:
This can happen if the context you used comes from a widget above the BlocProvider.
The context used was: BlocBuilder(dirty, state: _BlocBuilderBaseState#c9263(lifecycle state: created))
The relevant error-causing widget was
MaterialApp lib/src/app.dart:57
The line 57 is the return MaterialApp(..) which have the BlocBuilder inside.
So i don't know what is happening :S

I added the bloc provider. You can try this.

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: buildTheme(),
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
          bloc: BlocProvider.of<AuthenticationBloc>(context),
          builder: (context, state) {
            if (state is AuthenticationAuthenticated) {
              return HomeScreen();
            }
            if (state is AuthenticationUnauthenticated) {
              return LoginScreen(user: user);
            }
            if (state is AuthenticationLoading) {
              return LoadingIndicator();
            }
            return SplashScreen();
          }),

    );
  }

I added the bloc for the BlocBuilder and build the app again and the problem still happening... so i don't know what is the main reason of this issue..

@fjugaldev can you share a link to your app?

This is my repo.. @felangel https://github.com/fjugaldev/otunus-app use the INLABS-1 branch..

What you will see is an experimental project.. so don't be afraid about you will see in there.. hahahaha i'm new with flutter so..

@fjugaldev I opened a pull request to fix your issue. The problem was you had two mains defined and the one you were actually using didn't wrap the App in a BlocProvider.

Hope that helps! ๐Ÿ‘

Thanks so much @felangel.. it was that.. now everything is working.. thanks for your time and help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rsnider19 picture rsnider19  ยท  3Comments

hivesey picture hivesey  ยท  3Comments

Reidond picture Reidond  ยท  3Comments

RobPFarley picture RobPFarley  ยท  3Comments

wheel1992 picture wheel1992  ยท  3Comments