简体   繁体   English

我如何将 main.dart 中的变量访问到颤振中的其他页面,我正在使用 Getx 状态管理

[英]How can i access to a variable in main.dart to other pages in flutter, i am using Getx state management

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如何使用 Getx 状态管理将 main.dart 中的变量访问到其他页面,在这里我想将 main.dart 中的 localMemberid 设置为全局,以便从任何地方访问或将其传递给其他页面,这是正确的方法吗?使用安全存储来存储数据

main.dart主要.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());
    }
  }
}

Uplift your variable from the main function and make it Rx:从主函数中提升您的变量并使其成为 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; //然后在你的 LoginController 上删除String? localMemberid; // String? localMemberid; // String? localMemberid; // and import main.dart : String? localMemberid; //并导入main.dart

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的可观察变量无法使用 GetX 更新 Flutter 中的“main.dart”? - Why my observable variable can not update the "main.dart" in Flutter using GetX? Flutter:如何将 main.dart(入口点)更改为 Flutter 中的其他页面? - Flutter: How do I change main.dart(entry point) to some other page in Flutter? 如何在同一 lib 文件夹中的其他 screen.dart 文件中使用 main.dart 中的 flutter 变量? - How do i use flutter variables from main.dart in other screen.dart files in the same lib folder? 如何在我的 main.dart 文件中使用 showChoiceDialog() - how can I use showChoiceDialog() in my main.dart file 如何在我的 JS 文件中使用 main.dart 变量? - How can I use main.dart variable in my JS file? 这是我每次尝试运行 flutter 在 main.dart 中提供的代码时遇到的错误 - this is the error I am getting every time I try to run the code that flutter provides in main.dart 我正在尝试通过 main.dart 通过开始屏幕导航主屏幕,但无法导航。 我是新来的,无法理解这个错误。 PL - I am trying to navigate home screen via Start screen through main.dart but failed to navigate. I am new to flutter and can't understand this error. Pl Flutter:如何使用 Getx 更改变量? - Flutter : How can I change variable with Getx? 如果我只使用 flutter web(没有 android 或 ios 应用程序),我还应该在 index.html 和 main.dart 中两次初始化 firebase 应用程序吗? - should I still initialize firebase app twice in both index.html and main.dart if i am using only flutter web(no android or ios app)? 我如何在 flutter main.dart 中合并两个 home 函数 - How do I incorporate two home functions in flutter main.dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM