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
replace the "context" with "_" in your
ConditionalBuilder.builder
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 usecontext.read()
from flutter_bloc library insteadPlease try below code :