Home > OS >  Widget unique ID (identifier) in flutter?
Widget unique ID (identifier) in flutter?

Time:12-27

Simple question for which it was impossible to me to find an answer.

I am from a web background where you can identify an UI component in html by an id which is unique.

How can we uniquely identify widgets in flutter ?


My goal:

The main goal behind it is to make statistic about components of an app and track user interaction through versioned component specification.


My researches:

I have saw this question: the title describes exactly my problem but unfortunately the description and responses isn't at all!

I also couldn't find any reference in the flutter doc. It doesn't seem like a most used feature...or I may have been in the wrong places

CodePudding user response:

You can simply use a UniqueKey(). As it’s name suggests a unique key is a key that can uniquely identify a widget in flutter. You just have to instantiate one, and pass it to the « key: » property of a widget. And you’re then able to access your widget’s data through that key, kind of like in html.

CodePudding user response:

Well as you took a reference from HTML in html we pass a id to elements manually or dynamically as well using some logic. In Flutter you can assign a Unique id to every widget but you also have to declare it first to get the values inside the key.

In Flutter you have two types of keys Local and Global.

Just define a key and pass that to any widget once the widget renders over the screen you can get values of the widget using that key.

  • Related