Dart Duration not Behaving as Expected

65 Views Asked by At

I'm using FlutterFlow and have to add in a custom function to determine if a user should make a payment if the date is passed the exceeded date range. For example, if a user selected "Every 2 Weeks" for the variable everySoOften, it should read it as an interval of two weeks. If the datePicked is within two weeks of the current date, return false. Otherwise return true. My code now is returning true every time which is not expected. Any help would be appriciated!

import 'dart:convert';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:timeago/timeago.dart' as timeago;
import '/flutter_flow/lat_lng.dart';
import '/flutter_flow/place.dart';
import '/flutter_flow/uploaded_file.dart';
import '/flutter_flow/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/backend/supabase/supabase.dart';
import '/auth/supabase_auth/auth_util.dart';

bool? newCustomFunction(
  DateTime? datePicked,
  String? everySoOften,
) {
  /// MODIFY CODE ONLY BELOW THIS LINE
  String frequency = everySoOften?.toLowerCase() ?? '';

  int interval =
      int.parse(RegExp(r'\d+').firstMatch(frequency)?.group(0) ?? '0');

  String timeUnit = frequency.replaceAll(RegExp(r'\d+'), '').trim();

  Duration duration = Duration();

  switch (timeUnit) {
    case 'week':
    case 'weeks':
      duration = Duration(days: interval * 7); // 1 week = 7 days
      break;
    case 'month':
    case 'months':
      duration = Duration(days: interval * 30);
      break;
    case '3 months':
      duration = Duration(days: interval * 30 * 3);
      break;
    case '6 months':
      duration = Duration(days: interval * 30 * 6);
      break;
    case 'year':
    case 'years':
      duration = Duration(days: interval * 365);
      break;
  }

  bool isBeyondAllowance =
      DateTime.now().isAfter(datePicked?.add(duration) ?? DateTime.now());

  return isBeyondAllowance;

  /// MODIFY CODE ONLY ABOVE THIS LINE
}
0

There are 0 best solutions below