Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter- Is it safe to use static variable for userId

Tags:

flutter

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!

like image 603
Daibaku Avatar asked Sep 20 '25 05:09

Daibaku


2 Answers

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.

like image 85
Rémi Rousselet Avatar answered Sep 22 '25 18:09

Rémi Rousselet


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.

like image 30
Thanthu Avatar answered Sep 22 '25 20:09

Thanthu