如何解决在Flutter中使用Bloc / Cubit时避开CONTEXT
我正在颤抖一阵子,并且一直在使用setState()。最近决定学习BLoC。现在在v6中,Bloc随Cubit一起提供,因此开始跟进BLoC-Cubit的教程,了解它们的工作方式和实现方法。到目前为止,无论我学到什么,我都在一个演示项目上进行了实施。
我遇到了这个错误:
在不包含Cubit类型Cubit的上下文中调用BlocProvider.of()。
从传递给BlocProvider.of
()的上下文开始,找不到祖先。 如果您使用的上下文来自BlocProvider上方的小部件,则会发生这种情况。
使用的上下文是:BlocConsumer
(脏)
The relevant error-causing widget was BlocConsumer<Cubit<dynamic>,dynamic>
这会将我重定向到CreateProfile.dart行号。 3,在下面检查。
我猜该错误与上下文有关,我没有将正确的上下文传递给CreateProfile的BlocConsumer。但是,“登录”和“ Otp”页面中的“ Bloc”工作正常。
小部件树变为:Main-> Splash-> Login-> Otp-> CreateProfile。
如果我的方法不合适,请帮助我解决此问题,并提出正确的集团实施实践。
main.dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,home: Splash(),routes: {
Splash.id : (context) => Splash(),},);
}
}
Splash.dart
FlatButton(
onTap: (){
Navigator.push(context,MaterialPageRoute(builder: (context) => BlocProvider(
create: (context) => LoginCubit(),child: Login(),)));
},child: Text('Get Started',style: kText18SemiBold.copyWith(color: Colors.white)),),
Login.dart
return Scaffold(
backgroundColor: kBackgroundColor,body: BlocConsumer<LoginCubit,LoginState>(
listener: (context,state) {
if(state is LoginApiSuccess){
Navigator.push(context,MaterialPageRoute(builder: (context) => BlocProvider(
create: (context) => OtpCubit(),child: Otp(),)));
}
},builder: (context,state) {
return Stack(
// Contains Login UI
)
}
)
);
Otp.dart
return Scaffold(
backgroundColor: kBackgroundColor,body: BlocConsumer<OtpCubit,OtpState>(
listener: (context,state) {
if(state is OtpApiSuccess){
Navigator.push(context,MaterialPageRoute(builder: (context) => BlocProvider(
create: (context) => CreateprofileCubit(),child: CreateProfile(),state) {
return Stack(
// Contains OTP UI
)
}
)
);
CreateProfile.dart
return Scaffold(
backgroundColor: kBackgroundColor,body: BlocConsumer<CreateprofileCubit,CreateprofileState>( // Error redirects here
listener: (context,state) {
if(state is ProfileCreated){
Navigator.push(context,MaterialPageRoute(builder: (context) => AddProfileImages(),));
}
},state) {
return Stack(
// Contains CreateProfile UI
)
}
)
);
解决方法
BlocProvider是泛型类,您需要为其提供类型。尝试更换:
var guildList = bot.guilds.cache; // go through the cache property
try {
let messageToSend = new Discord.MessageEmbed()
.setTitle("Hello,you don't see me messaging in your server often...")
.setDescription(
`I have just flown in to tell you that my developers have something to say: \n ${msg}`
);
guildList.forEach((guild) => {
const channel = guild.channels.cache.find((channel) => channel.type === 'text') // try to find the channel
if (!channel) return; // if it couldn't find a text channel,skip this guild
channel.send(messageToSend); // otherwise,send the message
});
} catch (err) {
console.log(err);
}
具有:
BlocProvider(
create: (context) => OtpCubit(),child: Otp(),)
同一件事适用于您使用的所有其他BlocProvider。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。