BuildContext与状态上下文 [英] BuildContext vs State Context

查看:69
本文介绍了BuildContext与状态上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到状态的构建方法传入BuildContext,并且我还注意到状态本身也有一个称为context的成员.我想知道何时使用BuildContext,何时使用成员变量?它们可以互换吗?

I've noticed that a state's build method passes in a BuildContext, and I've also noticed that the State itself also has a member called context. I was wondering when it's appropriate to use the BuildContext, and when is it appropriate to use the member variable? Are they interchangeable?

有时候使用其中一个会导致错误吗?我们该如何采取措施确保不这样做?

Are there times when using one over the other can cause errors and how do we take measures to insure we don't do this?

推荐答案

扑朔迷离 State的文档和构建功能:

From the flutter documentation for State and the build function:

BuildContext参数始终与此State对象的context属性相同,并且在该对象的生存期内将保持不变.此处多余地提供了BuildContext参数,以便此方法与WidgetBuilder的签名匹配.

The BuildContext argument is always the same as the context property of this State object and will remain the same for the lifetime of this object. The BuildContext argument is provided redundantly here so that this method matches the signature for a WidgetBuilder.

这篇关于BuildContext与状态上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆