Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] 为什么文档中说Bloc无法真正的跨页面交互 #18

Closed
aitsuki opened this issue Aug 19, 2021 · 3 comments
Closed

[Question] 为什么文档中说Bloc无法真正的跨页面交互 #18

aitsuki opened this issue Aug 19, 2021 · 3 comments

Comments

@aitsuki
Copy link

aitsuki commented Aug 19, 2021

这两天想迁移到GetX,从Getx项目链接过来的,看到文档的开头我有点不解:

When using Bloc, there is a problem that I have been very concerned about so far, it is impossible to truly cross- page interaction! After repeatedly consulting official documents, a global Bloc method was used to achieve "pseudo" cross- page interaction.

为什么说Bloc无法做夸页面交互,文档中的final JumpOneLogic logic = Get.put(JumpOneLogic());final JumpTwoLogic twoLogic = Get.put(JumpTwoLogic()); 不就是等价于两个局部的BlocProvider吗,Bloc自身就使用Provider库作为依赖管理,应该是能实现文档中跨页面交互的需求,而不需要使用全局路由。当然,GetX能提供更清晰易用的依赖管理倒是真的。

@xdd666t
Copy link
Owner

xdd666t commented Aug 19, 2021

Bloc和Provider都是一样,基于InheritedWidget去传递数据,但是InheritedWidget传递数据,必须是基于父子节点的关系才能传递数据,页面之间的跳转,A -> B,B页面并不是A的子节点,他们俩更像是平级的关系,所以A页面BuilderProvider的Provider,我无法在B页面通过Provider.of的方式获取(不使用特殊方法:传递provider实例,A页面跳转路由里定义B页面BuilderProvider)

所以在Provider和Bloc中,想做跨页面交互,就需要把实例放在路由的顶节点或页面顶节点,这样可以做的跨页面交互,但是有个很不好的体验,这些全局的Provider实例不会随着页面的销毁而销毁,会全局存在,在实际业务场景中会非常的麻烦(某些交互模块进入页面要初始化数据)

@xdd666t
Copy link
Owner

xdd666t commented Aug 19, 2021

这篇文章前一部分,写了对InheritedWidget的一些思考:https://juejin.cn/post/6984593635681517582

@aitsuki
Copy link
Author

aitsuki commented Aug 19, 2021

好吧,不用“特殊”方法的话没法做到,官方文档就是这么使用的,不过如果是命名路由的话确实就比较难办了。

再说点题外话,即使Get.put + find的方式这种方式也觉得不是什么好办法,当前页面没法明确的知道自己可以使用哪些依赖,自己被“注入”了哪些依赖,它只是将依赖放进一个容器中,然后我们主动拉取,和InheritedWidget没有实质上的差别,只是更简单易用而已。所以我一直觉得flutter的“依赖注入”都有点不太好使,而是像文档那样描述为依赖管理。

总而言之谢谢大佬的解答,文章都很值得仔细阅读。

@aitsuki aitsuki closed this as completed Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants