Hello,
I am a beginner FlutterFlow user and I am asking for help. I want to create a small dating app. I don’t know the Dart programming language, so I used ChatGPT to write me a custom function code. The purpose of the function is to create a list of users who meet certain criteria.
Operation:
The authenticated user fills out a form specifying the following criteria: a) minimum age of the person sought (ageFrom), b) maximum age of the person sought (ageTo), c) the town where the person sought lives, d) information about alcohol, e) information about cigarettes.
The authenticated user can indicate all criteria or only some of them.
Based on the user’s date of birth, which is in Firebase in the users collection in the date_of_birthday field, the user’s age is calculated.
After validating the FlutterFlow code, no errors are shown, but after going to Test Function, an error message appears:
Compilation error: Info: Compiling with sound null safety
lib/main.dart:22:3:
Error: Undefined name 'FirebaseFirestore'.
FirebaseFirestore.instance.collection('users').get().then((querySnapshot) {
^^^^^^^^^^^^^^^^^
E rror: Compilation failed.
Function code:
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';
List<String> searchUsers(
int? ageFrom,
int? ageTo,
String? location,
String? alcohol,
String? cigarettes,
String? children,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
List<String> users = [];
final now = DateTime.now();
FirebaseFirestore.instance.collection('users').get().then((querySnapshot) {
for (var doc in querySnapshot.docs) {
final user = doc.data();
final dob = user['date_of_birthday'].toDate();
var age = now.year - dob.year;
if (dob.month > now.month ||
(dob.month == now.month && dob.day > now.day)) {
age--;
}
if (ageFrom != null && age < ageFrom) continue;
if (ageTo != null && age > ageTo) continue;
if (location != null && !location.contains(user['location'])) continue;
if (alcohol != null && user['alcohol'] != alcohol) continue;
if (cigarettes != null && user['cigarettes'] != cigarettes) continue;
if (children != null && user['children'] != children) continue;
users.add(user['name']);
}
});
return users;
/// MODIFY CODE ONLY ABOVE THIS LINE
}
How to solve this problem?