What is the difference between `FocusScope.of(context).unfocus()` and `FocusManager.instance.primaryFocus?.unfocus()` in the flutter?

Viewed 80

When using FocusScope.of(context).unfocus(), there was an bug that the screen was rebuild then data disappeared.

I fixed the bug by Modify FocusScope.of(context).unfocus() -> FocusManager.instance.primaryFocus?.unfocus()

but I still wonder the difference between the two code

thank you

1 Answers

FocusManager.instance.primaryFocus property contains the currently focused focus node, which is accessible globally independently of context.

FocusScope.of(context) returns the FocusScopeNode of the FocusScope that most tightly encloses the given context. Calling this function creates a dependency that will rebuild the given context when the focus changes.

Related