-1

So im trying to get an array of timestamps from 2 times in flutterflow. ex. time1 06:00, time2 06:05 the result would be 06:00, 06:01, 06:02, 06:03, 06:04, 06:05.

Im using a custom function so somebody please help

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/custom_functions.dart';
import '/backend/backend.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '/auth/firebase_auth/auth_util.dart';

List<DateTime> occupiedTime(
  DateTime from,
  DateTime to,
) {
  /// MODIFY CODE ONLY BELOW THIS LINE

  List<DateTime> occupiedTime(
    DateTime from,
    DateTime to, [
    Duration duration = const Duration(minutes: 1),
  ]) {
    List<DateTime> timestamps = [];

    while (from.isBefore(to) || from.isAtSameMomentAs(to)) {
      timestamps.add(from);
      from = from.add(duration);
    }

    return timestamps;
  }

  /// MODIFY CODE ONLY ABOVE THIS LINE
}


i tried this code but no matter what i try to return it gives me error

Con Other
  • 1
  • 1

2 Answers2

0

Based on your input & output, you are trying to get List time (interval based on minute).

It can be

  List<DateTime> occupiedTime(DateTime from, DateTime to) {
    final differenceInMinutes = to.difference(from).inMinutes;
    return List.generate(differenceInMinutes, (index) {
      return from.add(Duration(minutes: index));
    });
  }

While you like to format, you can use packages/intl or

  String getTimeString(DateTime dateTime) {
    return '${dateTime.hour}:${dateTime.minute}';
  }

Test snippet

  final result = occupiedTime(DateTime.now(), DateTime.now().add(Duration(minutes: 5)));
  final stringTime = result.map((e) => getTimeString(e)).toList();
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

You can define a method like this:

List<DateTime> occupiedTime(
  DateTime from,
  DateTime to, [
  Duration duration = const Duration(minutes: 1),
]) {
  List<DateTime> timestamps = [];

  while (from.isBefore(to) || from.isAtSameMomentAs(to)) {
    timestamps.add(from);
    from = from.add(duration);
  }

  return timestamps;
}

Usage:

void main() {
  DateTime time1 = DateTime(2023, 1, 1, 6, 0);
  DateTime time2 = DateTime(2023, 1, 1, 6, 5);

  List<DateTime> result = occupiedTime(time1, time2);

  for (DateTime timestamp in result) {
    print(timestamp.toString());
  }
}

Output:

2023-01-01 06:00:00.000
2023-01-01 06:01:00.000
2023-01-01 06:02:00.000
2023-01-01 06:03:00.000
2023-01-01 06:04:00.000
2023-01-01 06:05:00.000
Hamed
  • 5,867
  • 4
  • 32
  • 56
  • Thanks guys for the feedback, i tried Hamed's solutions and it gives me this warning, The body might complete normally, causing 'null' to be returned, but the return type, 'List', is a potentially non-nullable type. Try adding either a return or a throw statement at the end. I tried giving it the nullable option but when i test run it returns null value – Con Other May 19 '23 at 10:19
  • @ConOther please add your code. – Hamed May 19 '23 at 15:06
  • i updated the code in the thread @Hamed – Con Other May 19 '23 at 21:49