Home > front end >  How save and write data using GetX storage?
How save and write data using GetX storage?

Time:09-22

First I created the GetxController class

  final languageController = GetStorage();

  var myLocal = [];

  void saveLocale(List list) {
    languageController.write('savedLocale', list);
  }

  @override
  void onInit() {
    List<dynamic>? savedLocale = languageController.read('savedLocale');
    if (savedLocale != null) {
      myLocal = savedLocale;
    }
    super.onInit();
  }
}

Then I initialized GetStorage in main.dart

final myLocal = LanguageController().myLocal;

void main() async {
  print(myLocal);
  await GetStorage.init();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      translations: LocaleString(),
      locale: myLocal.isNotEmpty
          ? Locale(myLocal[0], myLocal[1])
          : Locale('en', 'US'),
      debugShowCheckedModeBanner: false,
      home: HomeScreen(),
    );
  }
}

And then in the dialog after setting the locale I writes it in storage

Future<dynamic> myMaterialDialog(BuildContext context) {
  final LanguageController languageController = Get.find();

  return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(chooseLanguage.tr),
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              TextButton(
                onPressed: () {
                  Get.back();
                  Get.updateLocale(Locale('en', 'US'));
                  languageController.saveLocale(['en', 'US']);
                },
                child: Text(englishLanguage.tr),
              ),
              TextButton(
                onPressed: () {
                  Get.back();
                  Get.updateLocale(Locale('ru', 'RU'));
                  languageController.saveLocale(['ru', 'RU']);
                },
                child: Text(russianLanguage.tr),
              ),
            ],
          ),
        );
      });
}

And it's not working, every time I restarted my app it's shows 1 what myLocale is empty

To check if saveLocale() method is working, I created printSavedLocale() method

 void printSavedLocale() {
    print(languageController.read('savedLocale'));
  }

and put it to dialoge button after saveLocale() and it's printing my saved locale, but after restarting saved locale is null

CodePudding user response:

use this static methods. put them anywhere in your project:

void setData(String key, dynamic value) => GetStorage().write(key, value);

int? getInt(String key) => GetStorage().read(key);

String? getString(String key) => GetStorage().read(key);

bool? getBool(String key) => GetStorage().read(key);

double? getDouble(String key) => GetStorage().read(key);

dynamic getData(String key) => GetStorage().read(key);

void clearData() async => GetStorage().erase();

CodePudding user response:

I can solove this by reading from the storage directly from main.dart

final LanguageController languageController = Get.put(LanguageController());
final myLocal = LanguageController().readSavedLocale();

void main() async {
  await GetStorage.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      translations: LocaleString(),
      locale: myLocal.isNotEmpty
          ? Locale(myLocal[0], myLocal[1])
          : Locale('en', 'US'),
      debugShowCheckedModeBanner: false,
      home: HomeScreen(),
    );
  }
}

And readSavedLocale() method is

List readSavedLocale() {
    var savedLocale = languageController.read('savedLocale');
    return savedLocale;
  }
  • Related