checkData function

void checkData({
  1. required dynamic data,
  2. required BuildContext context,
  3. required String successMsg,
  4. required String errorMsg,
  5. required BehaviorSubject<bool> listener,
  6. required bool listenerValue,
  7. required bool popContext,
  8. required Function? successCallback,
})

checkData checks the validity of the data returned from the api and proceeds if successful or shows an error message if there are any errors

variables passed in are custom

data - the data to be checked against context - the context successMsg - the message to be displayed on successful completion errorMsg - the message to be displayed in case of any error listeners - the listener to be updated listenerValue - the value to update the listener with

Implementation

void checkData({
  required dynamic data,
  required BuildContext context,
  required String successMsg,
  required String errorMsg,
  required BehaviorSubject<bool> listener,
  required bool listenerValue,
  required bool popContext,
  required Function? successCallback,
}) {
  if (data != null) {
    /// Update listeners
    listener.add(listenerValue);

    /// Close drawer
    if (popContext) {
      Navigator.pop(context);

      ///  Success feedback
      ScaffoldMessenger.of(context)
        ..hideCurrentSnackBar()
        ..showSnackBar(SnackBar(
            content: Text(successMsg), duration: const Duration(seconds: 5)));
    } else if (successCallback != null) {
      successCallback();

      /// Success feedback
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text(successMsg), duration: const Duration(seconds: 5)));
    }
    return;
  } else {
    /// Notify the user of the error
    ScaffoldMessenger.of(context)
      ..hideCurrentSnackBar()
      ..showSnackBar(SnackBar(
          content: Text(errorMsg), duration: const Duration(seconds: 10)));
  }
}