我如何访问main中的变量。到扑面而前的其他页面,我正在使用getx状态管理

发布于 2025-02-10 20:01:44 字数 1713 浏览 1 评论 0原文

我如何访问main中的变量。以getx状态管理的扑面而来的其他页面,我想在Main中进行localmemberid。使用安全存储存储数据

main.dart

void main() {
  SecureStorage secureStorage = SecureStorage();
  var localMemberid; // i would like to make this varial global or pass this value to other pages

  runApp(
    ScreenUtilInit(
      builder: (BuildContext context, Widget? child) {
        return GetMaterialApp(
          title: "onyx",
          initialRoute: AppPages.INITIAL,
          getPages: AppPages.routes,
          theme: ThemeData(primarySwatch: MaterialColor(0xFF0456E5, color)),
        );
      },
    ),
  );

  SecureStorage.readLocalSecureData('memberid')
      .then((value) => localMemberid = value);
}

登录控制器

class LoginController extends GetxController {
  final AuthenticationRepo _authRepe = AuthenticationRepo();
  final SecureStorage secureStorage = SecureStorage();
  String? localMemberid; // i would like to get the localMemberid from the main.dart

  //TODO: Implement LoginController

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {}

  var userid;
  var password;
  
 

  onSinginButton() async {
    var res = await _authRepe.login(username: userid, password: password);
    if (res.status == ApiResponseStatus.completed) {
      print(res.data);
      await SecureStorage.writeLocalSecureData('memberid', res.data!.memberid);
      localMemberid == null
          ? Get.toNamed(Routes.LOGIN)
          : Get.toNamed(Routes.HOME);
    } else {
      Get.defaultDialog(title: res.message.toString());
    }
  }
}

How can I access a variable in main.dart to other pages in flutter with Getx state management, Here I want to make the localMemberid in main.dart as Global to access from anywhere or pass it to other pages and is it the right way to use secure storage for storing the data

main.dart

void main() {
  SecureStorage secureStorage = SecureStorage();
  var localMemberid; // i would like to make this varial global or pass this value to other pages

  runApp(
    ScreenUtilInit(
      builder: (BuildContext context, Widget? child) {
        return GetMaterialApp(
          title: "onyx",
          initialRoute: AppPages.INITIAL,
          getPages: AppPages.routes,
          theme: ThemeData(primarySwatch: MaterialColor(0xFF0456E5, color)),
        );
      },
    ),
  );

  SecureStorage.readLocalSecureData('memberid')
      .then((value) => localMemberid = value);
}

Login Controller

class LoginController extends GetxController {
  final AuthenticationRepo _authRepe = AuthenticationRepo();
  final SecureStorage secureStorage = SecureStorage();
  String? localMemberid; // i would like to get the localMemberid from the main.dart

  //TODO: Implement LoginController

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {}

  var userid;
  var password;
  
 

  onSinginButton() async {
    var res = await _authRepe.login(username: userid, password: password);
    if (res.status == ApiResponseStatus.completed) {
      print(res.data);
      await SecureStorage.writeLocalSecureData('memberid', res.data!.memberid);
      localMemberid == null
          ? Get.toNamed(Routes.LOGIN)
          : Get.toNamed(Routes.HOME);
    } else {
      Get.defaultDialog(title: res.message.toString());
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2025-02-17 20:01:44

从主函数上升起变量,然后使其成为RX:

  var localMemberid=Rxn<String>(); // i would like to make this varial global or pass this value to other pages

void main() {
     SecureStorage secureStorage = SecureStorage();

     .......

   SecureStorage.readLocalSecureData('memberid')
      .then((value) => localMemberid.value = value);
   }

然后在loginController上删除字符串? localmemberid; //和导入main.dart

localMemberid.value == null
      ? Get.toNamed(Routes.LOGIN)
      : Get.toNamed(Routes.HOME);

Uplift your variable from the main function and make it Rx:

  var localMemberid=Rxn<String>(); // i would like to make this varial global or pass this value to other pages

void main() {
     SecureStorage secureStorage = SecureStorage();

     .......

   SecureStorage.readLocalSecureData('memberid')
      .then((value) => localMemberid.value = value);
   }

And then on your LoginController remove String? localMemberid; // and import main.dart:

localMemberid.value == null
      ? Get.toNamed(Routes.LOGIN)
      : Get.toNamed(Routes.HOME);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文