Flutter,如何在 try catch 块中使用 Get.off() 根据当前屏幕重定向到不同的屏幕

问题描述 投票:0回答:1

所以我有一个按钮可以调用

updateAllergy()
函数,并且它将重定向到主屏幕。假设我想将其重定向到另一个页面,该页面也有一个带有
updateAllergy()
功能的按钮,但两个屏幕都需要重定向到不同的屏幕。是否可以重定向到基于您当前屏幕的屏幕?我是 Flutter 的初学者,我希望我的问题措辞正确并且至少是可以理解的。

allergy_controller.dart

class AllergyController extends GetxController {
  static AllergyController get instance => Get.find();

  RxBool isVegan = false.obs;
  RxBool isDairyFree = false.obs;
  RxBool isEgg = false.obs;
  RxBool isNut = false.obs;
  final userController = UserController.instance;
  final userRepository = Get.put(UserRepository());
  GlobalKey<FormState> updateVeganFormKey = GlobalKey<FormState>();
  GlobalKey<FormState> updateDairyFormKey = GlobalKey<FormState>();
  GlobalKey<FormState> updateEggFormKey = GlobalKey<FormState>();
  GlobalKey<FormState> updateNutFormKey = GlobalKey<FormState>();

  // init user data when home appears
  @override
  void onInit() {
    initializeAllergy();
    super.onInit();
  }

  // fetch user record
  Future<void> initializeAllergy() async {
    isVegan.value =
        userController.user.value.preferences['Diet']['IsVegan'] ?? false;
    isDairyFree.value =
        userController.user.value.preferences['Diet']['IsDairyFree'] ?? false;
    isEgg.value =
        userController.user.value.preferences['Allergy']['IsEgg'] ?? false;
    isNut.value =
        userController.user.value.preferences['Allergy']['IsNut'] ?? false;
  }

  void saveVegan(bool? value) {
    isVegan.value = value ?? false;
  }

  void saveDairy(bool? value) {
    isDairyFree.value = value ?? false;
  }

  void saveEgg(bool? value) {
    isEgg.value = value ?? false;
  }

  void saveNut(bool? value) {
    isNut.value = value ?? false;
  }

  Future<void> updateAllergy() async {
    try {
      // form validation
      if (!updateVeganFormKey.currentState!.validate()) {
        updateVeganFormKey.currentState!.save();
        return;
      }

      if (!updateDairyFormKey.currentState!.validate()) {
        updateDairyFormKey.currentState!.save();
        return;
      }

      if (!updateEggFormKey.currentState!.validate()) {
        updateEggFormKey.currentState!.save();
        return;
      }

      if (!updateNutFormKey.currentState!.validate()) {
        updateNutFormKey.currentState!.save();
        return;
      }

      Map<String, dynamic> Preferences = {
        'Allergy': {
          'IsEgg': isEgg.value,
          'IsNut': isNut.value,
        },
        'Diet': {
          'IsVegan': isVegan.value,
          'IsDairyFree': isDairyFree.value,
        },
      };

      // Update the 'isVegan' field within the 'Diet' map
      Preferences['Diet']['IsVegan'] = isVegan.value;
      Preferences['Diet']['IsDairyFree'] = isDairyFree.value;
      Preferences['Diet']['IsEgg'] = isEgg.value;
      Preferences['Diet']['IsNut'] = isNut.value;

      // Create the data to update in Firestore
      Map<String, dynamic> allergyData = {
        'Preferences': Preferences,
      };
      await userRepository.updateSingleField(allergyData);

       // update the rx user value
      userController.user.value.preferences['Diet']['IsVegan'] = isVegan.value;
      userController.user.update((user) {
        user?.preferences['Diet']['IsVegan'] = isVegan.value;
      });

      userController.user.value.preferences['Diet']['IsDairyFree'] =
          isDairyFree.value;
      userController.user.update((user) {
        user?.preferences['Diet']['IsDairyFree'] = isDairyFree.value;
      });

      userController.user.value.preferences['IsEgg'] = isEgg.value;
      userController.user.update((user) {
        user?.preferences['IsEgg'] = isEgg.value;
      });

      userController.user.value.preferences['IsNut'] = isNut.value;
      userController.user.update((user) {
        user?.preferences['IsNut'] = isNut.value;
      });

      // move to previous screen
      Get.off(() => const NavigationMenu());
    } catch (e) {
      NLoaders.errorSnackBar(title: 'Oh Snap!', message: e.toString());
    }
  }
}
android flutter dart get
1个回答
0
投票

您可以将布尔值传递给

updateAllergy()
函数,并根据该布尔值将用户重定向到不同的屏幕。

这是

updateAllergy()
功能,最后您可以根据您的需要更新屏幕。

Future<void> updateAllergy(bool goToHome) async {
  try {
    // form validation
    if (!updateVeganFormKey.currentState!.validate()) {
      updateVeganFormKey.currentState!.save();
      return;
    }

    if (!updateDairyFormKey.currentState!.validate()) {
      updateDairyFormKey.currentState!.save();
      return;
    }

    if (!updateEggFormKey.currentState!.validate()) {
      updateEggFormKey.currentState!.save();
      return;
    }

    if (!updateNutFormKey.currentState!.validate()) {
      updateNutFormKey.currentState!.save();
      return;
    }

    Map<String, dynamic> Preferences = {
      'Allergy': {
        'IsEgg': isEgg.value,
        'IsNut': isNut.value,
      },
      'Diet': {
        'IsVegan': isVegan.value,
        'IsDairyFree': isDairyFree.value,
      },
    };

    // Update the 'isVegan' field within the 'Diet' map
    Preferences['Diet']['IsVegan'] = isVegan.value;
    Preferences['Diet']['IsDairyFree'] = isDairyFree.value;
    Preferences['Diet']['IsEgg'] = isEgg.value;
    Preferences['Diet']['IsNut'] = isNut.value;

    // Create the data to update in Firestore
    Map<String, dynamic> allergyData = {
      'Preferences': Preferences,
    };
    await userRepository.updateSingleField(allergyData);

    // update the rx user value
    userController.user.value.preferences['Diet']['IsVegan'] = isVegan.value;
    userController.user.update((user) {
      user?.preferences['Diet']['IsVegan'] = isVegan.value;
    });

    userController.user.value.preferences['Diet']['IsDairyFree'] =
        isDairyFree.value;
    userController.user.update((user) {
      user?.preferences['Diet']['IsDairyFree'] = isDairyFree.value;
    });

    userController.user.value.preferences['IsEgg'] = isEgg.value;
    userController.user.update((user) {
      user?.preferences['IsEgg'] = isEgg.value;
    });

    userController.user.value.preferences['IsNut'] = isNut.value;
    userController.user.update((user) {
      user?.preferences['IsNut'] = isNut.value;
    });

    // here check for the boolean value and navigate next screen
    if (goToHome) {
      // Navigate to home screen
    } else {
      // Navigate to other screen
    }
  } catch (e) {
    NLoaders.errorSnackBar(title: 'Oh Snap!', message: e.toString());
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.