Skip to content

Instantly share code, notes, and snippets.

@suleymanbilgin
Created October 26, 2020 11:39
Show Gist options
  • Save suleymanbilgin/53592cbc5c69cfea73833e70d62e195e to your computer and use it in GitHub Desktop.
Save suleymanbilgin/53592cbc5c69cfea73833e70d62e195e to your computer and use it in GitHub Desktop.
flutter GetStorage null
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:storagecont/storage_controller.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
StorageController.init().then((value) {
_getSavedLocale();
runApp(GetMaterialApp(
home: WowCardApp(),
),);
});
}
class WowCardApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Column(
children: [
RaisedButton(child: Text("get"), onPressed: () {
_getSavedLocale();
}),
RaisedButton(child: Text("set"), onPressed: () {
StorageController().setLocale("en");
}),
Text("suleyman"),
],
),
);
}
}
Locale _getSavedLocale() {
final String savedLocale = StorageController().getlocale();
final locale = (savedLocale == 'en')
? const Locale('en', 'US')
: const Locale('ar', 'AE');
return locale;
}
import 'package:get_storage/get_storage.dart';
class StorageController {
static Future<bool> init() async {
return await GetStorage.init().then((value) => value);
}
String getlocale() {
return GetStorage().read('locale') ?? '';
}
Future<void> setLocale(String newLocale)async {
GetStorage().write('locale', newLocale);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment