skip to Main Content

This class Cubit

import 'package:auth_todo/models/shared/cubit/states.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:sqflite/sqflite.dart';

class AppCubit extends Cubit<AppStates> {
  AppCubit() : super(AppInitialStates());
  static get(context) => BlocProvider.of(context);

  int selectedIndex = 0;

  List<Widget> screen = [
    const NewTasksScreen(),
    const ArchivedTasksScreen(),
    const DoneTasksScreen(),
  ];
  List<String> titles = [
    'New Tasks',
    'Done Tasks',
    'Archived Tasks',
  ];
  void changIndex(int index) {
    selectedIndex = index;
    emit(AppChangeBnvStates());
  }
  
}

class state

abstract class AppStates {}

class AppInitialStates extends AppStates {}

class AppChangeBnvStates extends AppStates {}

class AppCreateDataBaseState extends AppStates {}

class AppInsertToDataBaseState extends AppStates {}

class AppGetFromDataBaseState extends AppStates {}

class AppChangBottomSheetState extends AppStates {}

and this page To use BlocProvider

// ignore_for_file: must_be_immutable

import 'package:auth_todo/models/shared/cubit/states.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_nav_bar/google_nav_bar.dart';
import 'package:intl/intl.dart';
import '../models/shared/cubit/cubit.dart';
import '../widgets/components/dufalt_form_filed.dart';
import 'package:conditional_builder_null_safety/conditional_builder_null_safety.dart';

class HomeLayout extends StatelessWidget {

  TextEditingController taskTitelController = TextEditingController();
  TextEditingController taskTimeController = TextEditingController();
  TextEditingController taskDateController = TextEditingController();
  var scaffoldKey = GlobalKey<ScaffoldState>();
  var formKey = GlobalKey<FormState>();

  HomeLayout({super.key});

  @override
  Widget build(BuildContext context) {
    AppCubit cubit = AppCubit.get(context);
    return BlocProvider(
        create: (_) => AppCubit()..creatDatabase(),
        child: BlocConsumer<AppCubit, AppStates>(
          listener: (context, state) {
            if (state is AppInsertToDataBaseState) {
              Navigator.pop(context);
            }
          },
          builder: (context, state) => Scaffold(
            key: scaffoldKey,
            appBar: AppBar(
              title: Text(cubit.titles[cubit.selectedIndex]),
              centerTitle: true,
            ),
           
            body: ConditionalBuilder(
                condition: true,
                builder: (context) => cubit.screen[cubit.selectedIndex],
                fallback: (context) =>
                    const Center(child: CircularProgressIndicator())),
          ),
        ));
  }
}

Exception caught by widgets library

═══════════════════════════════════ The following assertion was thrown
building HomeLayout(dirty): BlocProvider.of() called with a context
that does not contain a StateStreamableSource<Object?>.

No ancestor could be found starting from the context that was passed
to BlocProvider.of<StateStreamableSource<Object?>>().

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

The context used was: HomeLayout(dirty)

The relevant error-causing widget was HomeLayout main.dart:31 When the
exception was thrown, this was the stack

2

Answers


  1. replace the "context" with "_" in your ConditionalBuilder.builder

    ConditionalBuilder(
        builder: (_) => context.read<readAppCubit>().screen[context.read<readAppCubit>().selectedIndex],
    )
    

    so that it takes the upper context wich provides your cubit

    note: remove the instanse of cubit at first line of your build method and use context.read() from flutter_bloc library instead

    Login or Signup to reply.
  2. Please try below code :

    BlocProvider(
            create: (_) => AppCubit.get(context)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search