skip to Main Content

I use GetStorage to store user data
, I want to read its data in user profile GetxController

 class UserHomeController extends GetxController {
  final box = GetStorage();
  // I found this method    **but**   i can't convert it to object
  // as   User get user => (userFromJson(GetStorage().read<User>('user'))
  String get userS => (GetStorage().read<String?>('user') ?? "");

  @override
  Future<void> onInit() async {
    super.onInit();
  }

Thanks

2

Answers


  1. Check the below code, you can directly read the data using read method with key name:

    import 'package:flutter/material.dart';
    import 'package:get/get.dart';
    import 'package:get_storage/get_storage.dart';
    
    
    class Controller extends GetxController {
      final box = GetStorage();
      bool get isDark => box.read('darkmode') ?? false;
      ThemeData get theme => isDark ? ThemeData.dark() : ThemeData.light();
      void changeTheme(bool val) => box.write('darkmode', val);
    }
    
    Login or Signup to reply.
  2.  class UserHomeController extends GetxController {
      final box = GetStorage();
       //Change the following line
       // String get userS => (GetStorage().read<String?>('user') ?? "");
       // to
       String get userS => (box.read('user') ?? "");
    
      @override
      Future<void> onInit() async {
        super.onInit();
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search