Hello I want to save name of Category in my application in shared_preferences that the name come from firebase
that the code of how I get my category
the idea of program that the user only use on category not all of them
in the first the user will chose the category I have to save the Category in the app that come from firebase
final CollectionReference _categoryCollectionRef =
FirebaseFirestore.instance.collection('Country');
Future<List<QueryDocumentSnapshot>> getCategory() async {
var value = await _categoryCollectionRef.get();
return value.docs;
}
getCategory() async {
_loading.value = true;
await HomeService().getCategory().then((value) => {
for (int i = 0; i < value.length; i )
{
_categoryModel.add(CategoryModel.fromJson(
value[i].data() as Map<String, dynamic>)),
_loading.value = false,
},
update(),
});
}
that the button I call it my function
GestureDetector(
onTap: () async {
Get.off(
ControlView(),
);
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: primary),
height: 50,
child: CustomText(
alignment: Alignment.center,
text: controller.categoryModel[index].name,
),
),
);
CodePudding user response:
You can use shared_preferences and do like this:
final prefs = await SharedPreferences.getInstance().reload();
prefs.setString('uniquekey', 'name of Category');
in your case:
onTap: () async {
final prefs = await SharedPreferences.getInstance().reload();
prefs.setString('categoryName', controller.categoryModel[index].name);
Get.off(
ControlView(),
);
},
and read it like this:
final prefs = await SharedPreferences.getInstance().reload();
String result = prefs.get('categoryName');
if(result != null){
print(result);
}