Need help with custom function to check if a msg is first of the day

Custom Code

Hi, I am trying to write a function that will check if a message is the first of the day.

The logic works, it print true for the first message, but the return does not - it always returns false.

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 '/auth/firebase_auth/auth_util.dart';

bool verifyFirstMessage2(
String chatRef,
DateTime? messageTs,
) {
/// MODIFY CODE ONLY BELOW THIS LINE

if (messageTs == null) {
return false;
}

final firestore = FirebaseFirestore.instance;
final chatDocRef = firestore.collection('chats').doc(chatRef);
final messagesColRef = chatDocRef.collection('messages');

final dayStart = DateTime(messageTs.year, messageTs.month, messageTs.day);
final dayEnd = DateTime(messageTs.year, messageTs.month, messageTs.day + 1);

messagesColRef
.where('ts', isGreaterThanOrEqualTo: Timestamp.fromDate(dayStart))
.where('ts', isLessThan: Timestamp.fromDate(dayEnd))
.orderBy('ts')
.limit(1)
.get()
.then((snapshot) {
if (snapshot.docs.isEmpty) {
print('false');
return false;
}

final firstMessage = snapshot.docs.first;
final firstMessageTs = firstMessage['ts'].toDate();

print('Chat ID: $chatRef');
print('Message ID: ${firstMessage.id}');
print('Timestamp: $firstMessageTs');

if (firstMessageTs.isAtSameMomentAs(messageTs)) {
print('true');
return true;
} else {
print('false');
return false;
}
});

return false;

/// MODIFY CODE ONLY ABOVE THIS LINE
}


What have you tried so far?

I have tried adding Future, await and async after asking Chat GPT. But now flutterflow does not let me save the function and I get error Errors found in custom function "verifyFirstMessage2".

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 '/auth/firebase_auth/auth_util.dart';

Future<bool> verifyFirstMessage2(
  String chatRef,
  DateTime? messageTs,
) async {
  /// MODIFY CODE ONLY BELOW THIS LINE

  if (messageTs == null) {
    return false;
  }

  final firestore = FirebaseFirestore.instance;
  final chatDocRef = firestore.collection('chats').doc(chatRef);
  final messagesColRef = chatDocRef.collection('messages');

  final dayStart = DateTime(messageTs.year, messageTs.month, messageTs.day);
  final dayEnd = DateTime(messageTs.year, messageTs.month, messageTs.day + 1);

  final snapshot = await messagesColRef
      .where('ts', isGreaterThanOrEqualTo: Timestamp.fromDate(dayStart))
      .where('ts', isLessThan: Timestamp.fromDate(dayEnd))
      .orderBy('ts')
      .limit(1)
      .get();

  if (snapshot.docs.isEmpty) {
    print('false');
    return false;
  }

  final firstMessage = snapshot.docs.first;
  final firstMessageTs = firstMessage['ts'].toDate();

  print('Chat ID: $chatRef');
  print('Message ID: ${firstMessage.id}');
  print('Timestamp: $firstMessageTs');

  if (firstMessageTs.isAtSameMomentAs(messageTs)) {
    print('true');
    return true;
  } else {
    print('false');
    return false;
  }

  /// MODIFY CODE ONLY ABOVE THIS LINE
}


Did you check FlutterFlow's Documentation for this topic?
Yes
21 replies