4

I want to know whether it's ok to do or I should not do. For example I created class like this:

class Utils {
    static String uid;
}

And get the user's uniqueId when user logged in and set it like so:

Utils.uid = uniqueId;

After that, just pass around. Let's say I have there tabs and starts with first tab which get uid here and when go to second tab, use uid for something. Is it something I should not do or is it ok? Any help is appreciated!

vahdet
  • 6,357
  • 9
  • 51
  • 106
Daibaku
  • 11,416
  • 22
  • 71
  • 108

3 Answers3

2

I can't really say wether it's right or wrong cause it depends, but what I am doing and I've seen several others do is to store the user related info like user id in SharedPreferences once the user logs into the application.

Hope this was helpful.

Thanthu
  • 4,399
  • 34
  • 43
2

No. In general inside Flutter, everything should be coming from a widget. Be it configurations, API data, authentification, or whatever you can think of.

While you could make it work with singletons/static properties, you loose a lot. By wrapping everything inside a widget; it ensures that whenever the data change, everything gets reloaded correctly.

In this situation you'll want to expose user informations using an InheritedWidget.

More informations here on how to use them.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • Thank you for the comment. I don't think I understand correctly. So let inheritedWidget hold uid and being inherited by basically every other widget(because uid is used almost everywhere). And every time I need uid, then get from inherited widget. Like a widget version of singleton. Am I understand right? – Daibaku Aug 17 '18 at 13:00
  • That's the idea. The difference being that whenever a value of `InheritedWidget` change, all widgets who depends from that inherited are automatically updated. – Rémi Rousselet Aug 17 '18 at 13:09
  • Now I understand. Thanks so much! – Daibaku Aug 17 '18 at 13:27
1

now you can use Providers to share data across the pages and widgets Providers reload only necessary widgets which keep top performance than static properties