Dicky Mahendra
Dicky Mahendra

Reputation: 1

ERROR BlocProvider.of() called with a context that does not contain a Bloc of type Bloc<dynamic, dynamic>

This is my main class main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'package:bwa_flutix/services/services.dart';
import 'bloc/bloc.dart';
import 'ui/pages/pages.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return StreamProvider.value(
        value: AuthServices.userStream,
        child: MultiBlocProvider(
            providers: [BlocProvider(create: (_) => PageBloc())],
            child: MaterialApp(
                debugShowCheckedModeBanner: false, home: Wrapper())));
  }
}
Error ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Wrapper(dependencies:
[_InheritedProviderScope<FirebaseUser>]):
BlocProvider.of() called with a context that does not contain a Bloc of type Bloc<dynamic, dynamic>.
No ancestor could be found starting from the context that was passed to
BlocProvider.of<Bloc<dynamic, dynamic>>().

This can happen if the context you used comes from a widget above the BlocProvider.
I/flutter (12415):
The context used was: BlocBuilder<Bloc<dynamic, dynamic>, dynamic>(dirty, state:
_BlocBuilderBaseState<Bloc<dynamic, dynamic>, dynamic>#840bd(lifecycle state: created))

The relevant error-causing widget was: Wrapper

wrapper.dart

part of 'pages.dart';

class Wrapper extends StatelessWidget {

  @override

Widget build(BuildContext context) {

FirebaseUser firebaseUser = Provider.of<FirebaseUser>(context);
if (firebaseUser == null) {
  if (!(prevPageEvent is GoToSplashPage)) {
    prevPageEvent = GoToSplashPage();
    context.bloc<PageBloc>().add(prevPageEvent);
  }
} else {
  if (!(prevPageEvent is GoToMainPage)) {
    prevPageEvent = GoToMainPage();
    context.bloc<PageBloc>().add(prevPageEvent);
  }
}
return BlocBuilder(
    builder: (_, pageState) => (pageState is OnSplashPage)
        ? SplashPage()
        : (pageState is OnLoginPage) ? SignInPage() : MainPage());}}

Upvotes: 0

Views: 1207

Answers (1)

KuKu
KuKu

Reputation: 7492

Would you try testing below code?

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: StreamProvider.value(
        value: AuthServices.userStream,
        child: MultiBlocProvider(
          providers: [BlocProvider(create: (_) => PageBloc())],
          child: Wrapper(),
        ),
      ),
    );
  }
}

Upvotes: 1

Related Questions