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

思考题:为什么build方法的参数不定义成Element对象,而要定义成BuildContext ? #323

Closed
evanwsu opened this issue Nov 8, 2019 · 2 comments

Comments

@evanwsu
Copy link

evanwsu commented Nov 8, 2019

这个问题可以探讨一下,不留下解答很难受呀!
先说下我的理解,使用BuildContext是不想将Element完全暴露出去,因为Element依赖Widget,如果参数使用Element, 则Widget也会依赖Element,这就导致互相依赖,形成了高耦合,使用BuildContext作为参数,可以避免互相依赖。

@wendux wendux changed the title 为什么build方法的参数不定义成Element对象,而要定义成BuildContext ? 思考题:为什么build方法的参数不定义成Element对象,而要定义成BuildContext ? Nov 25, 2019
@wendux wendux pinned this issue Nov 25, 2019
@wendux
Copy link
Member

wendux commented Nov 25, 2019

  1. 使用BuildContext是不想将Element完全暴露出去。Element接口非常多,有些是使用者不许要关心的,有些是在build阶段不能调用的,而在build时只需要暴露一些用户关心的和能够调用的接口。
  2. 相互依赖这个无法避免;即使参数定义为BuildContext,调用时传递的也是Element, 也是要依赖的。这个不是原因。

@evanwsu
Copy link
Author

evanwsu commented Nov 26, 2019

明白了,谢谢。

@evanwsu evanwsu closed this as completed Feb 19, 2020
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