[英]throw providernotfoundexception(t, context.widget.runtimetype);
I'm learning flutter and decided to work on a todo list application using cubit.我正在学习 flutter 并决定使用 cubit 开发待办事项列表应用程序。 I am created a cubit using bloc provider in the homescreen and in another screen I'm trying to consume the same cubit directly without creating another one.
我在主屏幕中使用 bloc provider 创建了一个肘,在另一个屏幕中我试图直接使用相同的肘而不创建另一个。
Homescreen cubit section and creating database using cubit:主屏幕 cubit 部分和使用 cubit 创建数据库:
I created the cubit here and created the database.我在这里创建了 cubit 并创建了数据库。
class Homescreen extends StatelessWidget {
const Homescreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => appcubit()..CreateDatabase(),
child: BlocConsumer<appcubit, appStates>(
listener: (context, state) {
// ignore: todo
// TODO: implement listener
},
builder: (context, state) {
appcubit cubit = appcubit.get(context);
return Scaffold(
I have a button that directs to a second page:我有一个指向第二页的按钮:
Widget buildTaskCat(tasknum, cat, progress, context) {
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => cattaskview(
cat: cat,
progress: progress,
tasknum: tasknum,
),
),
);
},
On the second page Im trying to consume the cubit without using bloc provider.在第二页上,我试图在不使用 bloc 提供程序的情况下使用 cubit。 When I use bloc provider somehow I cant access the data in the database and I have to call create database again.
当我以某种方式使用 bloc provider 时,我无法访问数据库中的数据,我必须再次调用创建数据库。
class cattaskview extends StatelessWidget {
const cattaskview(
{Key? key,
required this.cat,
required this.tasknum,
required this.progress})
: super(key: key);
final String cat;
final int tasknum;
final double progress;
@override
Widget build(BuildContext context) {
return BlocConsumer<appcubit, appStates>(
listener: (context, state) {
// TODO: implement listener
},
builder: (context, state) {
return Scaffold(
I get this error message when I try to run当我尝试运行时收到此错误消息
if (inheritedElement == null && null is! T) {
throw ProviderNotFoundException(T, context.widget.runtimeType);
}
return inheritedElement;
}
Have you tried using the BlocProvider.value()
Widget?您是否尝试过使用
BlocProvider.value()
小部件?
For example:例如:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: BlocProvider.of<appcubit>(context)
child: cattaskview(),
)
)
);
I fixed the issue by creating the cubit before material app我通过在材料应用程序之前创建 cubit 解决了这个问题
void main() {
Bloc.observer = MyBlocObserver();
runApp(const Todo());
}
class Todo extends StatelessWidget {
const Todo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => appcubit()..CreateDatabase(),
child: BlocConsumer<appcubit, appStates>(listener: (context, state) {
// TODO: implement listener
}, builder: (context, state) {
return MaterialApp(
theme: ThemeData(
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.light,
),
),
),
home: Homescreen(),
debugShowCheckedModeBanner: false);
}));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.