How to create a global value in flutter - flutter

I have a value store in Firebase config . When my app starts I would like to read the value and have it accessible from anywhere in my app.
I cannot use provider because I may not have the context. How can I do this in Flutter
Thanks

You can either store it in one dart file or in a class with variable with static field and whenever you need it you can grab it and read or change from there.
If it needs to stay even after you restart the app, you should use https://pub.dev/packages/shared_preferences package

Related

Flutter: Localization from API call

I wish to localize a Flutter application where locales are fetched by an API call, given the requested language.
I was hoping to be able to use the Intl package or something similar, but I am not sure this is possible without the .arb files.
Any ideas on how to accomplish this without reinventing the wheel?
(Having the localizations stored locally is not an option)
Down below, you can see a class which is converted to a singleton pattern. You can use any service locator package. It will be the same thing.
Now you can call this class in your main function, default set to EN.
Now let's say, you want to support SPANISH and not want to use .arb files
Now you can call google translate and replace values with the existing one. for every variable. I hope this helps.
Use https://pub.dev/packages/localizely_sdk package, it provides what you want to achieve
Turns out easy_localization has the functionality described. Simply creating a custom HttpAssetLoader and passing it to the easy_localization initialization method works out of the box, and provides device language detection, and application rebuild on locale change as intended.

Is it possible to get information from a terminal into a flutter app? If yes, please share an example

I'm writing a flutter app that needs to set up elasticsearch on first run. To do this, I need to run some commands in the terminal, I know how to do it, but I also need information from the terminal for a complete setup. How to get information from terminal to my flutter app, can i assign the value of one of the terminal strings to some variable in flutter?
I not found a some issues about getting info from terminal to app.
You can use --dart-define from the terminal while running your app or while building your application.
for example
flutter run --dart-define=BASE_URL=https://flutter.dev
Here BASE_URL is a key and you can pass value to it, you can name it as per your need.
And, You can access this in your Flutter app like below:
const String.fromEnvironment('BASE_URL')
You can check out this video as well for the same: Passing values from the command line to Flutter app
const is required while accessing that value you pass using dart-define and the key name is case sensitive.

Create a global variable for text size in Flutter

I'm developing a Flutter App, and one of the steps I need to take is to implement a way for user to increase/decrease the size of the text. For that, I'm thinking about making a slider in the settings page, which is quite straight-forward, but I don't know how to create a global value, change it (so it can't be a constant) and use it everywhere.
Any help is much is much appreciated !
You can store persistent data using shared_preferences or get_storage.
You can use the Hive framework to store the data locally on the device - https://docs.hivedb.dev/#/
First install Hive as a dependency and import it. Initialize Hive using Hive.initFlutter() (in the hive_flutter package).
Open a box using Hive.openBox("boxName"). Store the box as a variable - var box = Hive.box("boxName").
Add data into the box using box.put("key", "value").
Then you can access the data from anywhere you want by calling Hive.get("key").

Flutter Mobx - Passing store state between pages in PageView

How can i pass or preserve the state of my store between pages using PageView?
By example, I'm trying to upload an image at the first page to edit, and pass to a second page where i want to show the image with a form. Im using MVC, and the image is set via controller (store), but it's not working. I've been made some tests and sometimes, at debugging runtime, the image is shown at the second page. But, sometimes even debugging it isn't shown and its value is null.
I tried with AutomaticKeepAliveClientMixin also, but the behavior is the same.
Someone can tell me what i'm doing wrong or what i'm missing?
I think using singleton can solve your problem, actually I think I had this problem in two apps I made.
I used get_it plugin, this package creates singletons easily for you and it's simple to retrieve anywhere in the app without using context.
First, register your MobX as Singleton, I do it on main:
getIt.I.registerSingleton<YourStore>(YourStore());
Retrieve the same instance of your Store, if you have observables in the first page and call an action on the second page, it will trigger the observables in the first page or wherever you used any observable from this instance of Store.
GetIt.I<YourStore>().changeImageFromFirstPage();
You can call GetIt.I().youraction() anywhere in your app, it will trigger the observables you created on this Store.
final list = Provider.of<TodoList>(context);
in the MobX document, there is a provider that can be used as the above code.
I use GetIt too, it is really awesome.
there is a sample for this in the following URL for the MobX:
https://mobx.netlify.app/examples/todos

Access Flutter SharedPreferences in Swift

Is it possible to access SharedPreferences saved from Flutter accessed in Swift code of plugin? In Android we have FILE mode for SharedPreferences.
Any similar feature in Swift 4?
The shared_preferences uses NSUserDefaults on iOS to store the data. You can easily access it with Swift like this:
let name = NSUserDefaults.standard.string(forKey: "flutter.test")
print(name)
It would also make sense to use the optional binding to get the value safely:
if let name = NSUserDefaults.standard.string(forKey: "flutter.test") {
print(name)
}
Note, that if you use the key test in your flutter/dart code you would need to add the flutter. prefix to the key, as the shared_preferences plugin prefixes every key with it (see this line in the source code)
Use UserDefaults on Swift.
UserDefaults.standard.object(forKey:"flutter.key"))
key = key used em flutter to shared preferences.
You need to use flutter prefix on key.
I am not sure there exist anything like that, but you don't even need that.
You can fetch the value in Flutter itself, and then send the value using MethodChannel.