Created
October 26, 2020 11:39
-
-
Save suleymanbilgin/53592cbc5c69cfea73833e70d62e195e to your computer and use it in GitHub Desktop.
flutter GetStorage null
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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