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.
Have a look at the images. I'm trying to implement the android work manager natively via the method channel. Now, if we start the work manager, I need to call a method on the Flutter side.
I can't find a way to do that. I need flutterEngine for that, and that object should come from FlutterActivity. But there is no way to pass that to the work manager. It lets you pass only data types like int, bool, double, and string.
Flutter Activity:
Worker:
You can use the package or implement it similarly with the package
https://pub.dev/packages/workmanager
On flutter
write the top level function
when initialing app, get callback handle key with PluginUtilities.getCallbackFromHandle(Function callback)
send the callback handle key to Android
On Android
save the callback handle key with SharedPreferences
when you need, create FlutterEngine and run dart code(engine.dartExecutor.executeDartCallback) using the saved callback handle key
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
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").
In Android, if I have the information I want to persist across sessions I know I can use SharedPreferences or create a SQLite database or even write a file to the device and read it in later.
Is there a way to save and restore data like this just using Flutter? Or would I need to write device-specific code for Android and iOS like in the services example?
There are a few options:
Read and write files: https://flutter.io/reading-writing-files/
SQLite via a Flutter plugin: https://github.com/tekartik/sqflite
SQLCipher via a Flutter plugin: https://github.com/drydart/flutter_sqlcipher
SharedPreferences via a Flutter plugin: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
Localstore via a Flutter plugin: https://pub.dev/packages/localstore
If you are in a situation where you wanna save a small value that you wanna refer later. then you should store your data as key-value data using shared_preferences
Storing key-value data on disk
but if you want to store large data you should go with SQLITE
How to get Started with SQLITE in Flutter
however you can always use firebase database which is available offline
how to add firebase to your flutter project
Firebase for Flutter Codelab from google
Since we are talking about local storage you can always read and write files to the disk
Reading and Writing Files
Other solutions :
Simple Embedded Application Store database
A Flutter plugin to store data in secure storage
A late answer but I hope it will help anyone visiting here later too😁..
I will provide categories to save and their respective best methods...
Shared Preferences
Use this when storing simple values on storage e.g Color theme, app language, last scroll position(in reading apps).. these are simple settings that you would want to persist when the app restarts..
You could, however, use this to store large things(Lists, Maps, Images) but that would require serialization and deserialization.. To learn more on this deserialization and serialization go here.
Files
This helps a lot when you have data that is defined more by you for example log files, image files and maybe you want to export csv files.. I heard that this type of persistence can be washed by storage cleaners once disk runs out of space.. Am not sure as i have never seen it..
This also can store almost anything but with the help of serialization and deserialization..
Saving to a database
This is enormously helpful in data which is a bit complex. And I think this doesn't get washed up by disc cleaners as it is stored in AppData(for android)..
In this, your data is stored in an SQLite database. Its plugin is SQFLite.
Kinds of data that you might wanna put in here are like everything that can be represented by a database.
You can use shared preferences from flutter's official plugins.
https://github.com/flutter/plugins/tree/master/packages/shared_preferences
It uses Shared Preferences for Android, NSUserDefaults for iOS.
If you need to store just simple values like API token or login data (not passwords!), here is what I used:
import 'package:shared_preferences/shared_preferences.dart';
asyncFunc() async { // Async func to handle Futures easier; or use Future.then
SharedPreferences prefs = await SharedPreferences.getInstance();
}
...
// Set
prefs.setString('apiToken', token);
// Get
String token = prefs.getString('apiToken');
// Remove
prefs.remove('apiToken');
Don't forget to add shared_preferences dependency in your pubspec.yaml (preserve spacing format):
dependencies:
shared_preferences: any
You can use Localstorage
flutter pub add localstorage
1- Add dependency to pubspec.yaml (Change the version based on the last)
dependencies:
...
localstorage: ^4.0.0+1
2- Then run the following command
flutter packages get
3- import the localstorage :
import 'package:localstorage/localstorage.dart';
4- create an instance
class MainApp extends StatelessWidget {
final LocalStorage storage = new LocalStorage('localstorage_app');
...
}
Add item to lcoalstorage :
void addItemsToLocalStorage() {
storage.setItem('name', 'Abolfazl');
storage.setItem('family', 'Roshanzamir');
final info = json.encode({'name': 'Darush', 'family': 'Roshanzami'});
storage.setItem('info', info);
}
Get an item from lcoalstorage:
void getitemFromLocalStorage() {
final name = storage.getItem('name'); // Abolfazl
final family = storage.getItem('family'); // Roshanzamir
Map<String, dynamic> info = json.decode(storage.getItem('info'));
final info_name=info['name'];
final info_family=info['family'];
}
Delete an item from localstorage :
void removeItemFromLocalStorage() {
storage.deleteItem('name');
storage.deleteItem('family');
storage.deleteItem('info');
}
There are a few options:
Moor: Persistence library for Dart
https://pub.dev/packages/moor_flutter
Read and Write file
https://flutter.io/reading-writing-files/
Shared preferences plugin for flutter
https://pub.dev/packages/shared_preferences
SQlite for flutter
https://pub.dev/packages/sqflite
I was looking for the same, simple local storage but also with a reasonable level of security. The two solutions I've found that make the most sense are flutter_secure_storage (as mentioned by Raouf) for the small stuff, and hive for larger datasets.
I think If you are going to store large amount of data in local storage you can use sqflite library. It is very easy to setup and I have personally used for some test project and it works fine.
https://github.com/tekartik/sqflite
This a tutorial - https://proandroiddev.com/flutter-bookshelf-app-part-2-personal-notes-and-database-integration-a3b47a84c57
If you want to store data in cloud you can use firebase. It is solid service provide by google.
https://firebase.google.com/docs/flutter/setup
Hive (https://pub.dev/packages/hive) is very fast and flexible solution. But if you have experience with SQL; you can use SqfLite packages (https://pub.dev/packages/sqflite)