Using a single TextField to search for restaurants and food items.

Actions & Logic

Hello, FlutterFlow community, Please I need your assistance. I want to use this TextField to search for both restaurants and the food items in their menus. The logic is as follows:

  • When a user searches for a restaurant, it should display the relevant restaurants.

  • When they search for a food item, it should display the food item along with it corresponding restaurant.

All of this functionality should work using the same TextField. I am using a custom function to perform the search. Below is the custom function, and I’ve also attached screenshots for reference.

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/schema/structs/index.dart';

import '/backend/schema/enums/enums.dart';

import '/auth/firebase_auth/auth_util.dart';

bool textContaintext(

String searchIN,

String searchFOR,

) {

/// MODIFY CODE ONLY BELOW THIS LINE

return searchIN.toLowerCase().contains(searchFOR.toLowerCase());

/// MODIFY CODE ONLY ABOVE THIS LINE

}

What have you tried so far?

What I’ve tried so far is this: when a restaurant owner adds a new food item, I also save the name of the restaurant in the product collection (menu collection). However, when I tested it, I noticed the following issues:

  1. When I search for restaurants, the same restaurant is displayed multiple times.

  2. When I search for food items, if a single restaurant has added multiple food items with similar names, the restaurant is displayed repeatedly, instead of showing the restaurant once with the food items listed below it

The way I performed the search is as follows:

  1. I queried the product collection (food collection) as a list of documents on the page.

  2. I added a page state called "search", which is a list of documents.

  3. On the TextField, I added an "on change" action that filters the list of documents using the custom function I provided earlier

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