0

I have gone through multiple searching tutorials and articles but unfortunately i couldn't find any search method that can support Realtime database logic search in flutter, so is there any simple way to search Reealtime database in a TextField?

Here is my code:

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:idevice_me/providers/project_provider.dart';
import 'package:idevice_me/screens/user_detail_screen.dart';
import 'package:provider/provider.dart';



class HomeWidget extends StatefulWidget {
  const HomeWidget({ Key? key }) : super(key: key);

  @override
  State<HomeWidget> createState() => _HomeWidgetState();
}

class _HomeWidgetState extends State<HomeWidget> {

  
  var _isInit = true;
  
  @override
  void didChangeDependencies(){
    if(_isInit){
       Provider.of<ProjectProvider>(context, listen: false).readData();
    }
    _isInit = false;
    super.didChangeDependencies();
  }

   

  @override
  Widget build(BuildContext context) {
  TextEditingController searchData =  TextEditingController();
  var dbRef = FirebaseDatabase.instance.ref();
  
    return StreamBuilder(
      stream:  dbRef.child('users').onValue,
      builder: (ctx, AsyncSnapshot snapshot){
        if(snapshot.connectionState == ConnectionState.waiting){
                return const Center(child: CircularProgressIndicator());
              }
        if(snapshot.hasData){
          //----------------------------
          //USERID comes from here
          //----------------------------
         final userSnapshot = snapshot.data!.snapshot.children.toList();
          return Column(
            children: [
             const SizedBox(height: 20,),
              Padding(
                padding: const EdgeInsets.only(right: 15.0, left: 15.0),
                child: TextField(
                          decoration: InputDecoration(
                            labelText: 'Search',
                            labelStyle: TextStyle(fontSize: 14,color: Colors.grey.shade400,fontWeight: FontWeight.w600),
                            enabledBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(30),
                              borderSide: BorderSide(color: Colors.grey.shade300),
                            ),
                            focusedBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(30),
                              borderSide: const BorderSide(color: Colors.blue),
                            ),
                            floatingLabelBehavior: FloatingLabelBehavior.auto,
                          ),
                          key: const ValueKey('search'),
                            
                            keyboardType: TextInputType.text,

                            controller: searchData,
                          
                        ),
              ),
              const SizedBox(height: 10,),
              // ignore: deprecated_member_use
              FlatButton(
                onPressed: (){
                  setState(() {
                    
                  });
                }, 
              child: const Text('Search'),
              textColor: Colors.white,
              color: Colors.black,
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
              ),
              const Divider(),
                     const SizedBox(height: 20,),
              Expanded(
                child: ListView.builder(
                  itemCount: userSnapshot.length,
                  itemBuilder: (BuildContext ctx, i)=> Column(
                    children: [
                      // ignore: sized_box_for_whitespace
                      GestureDetector(
                        onTap: (){
                          Navigator.of(context).pushNamed(
                            UserDetailScreen.routeName, 

                            //--------------------------------------
                            //USERID should be here through snapshot
                            //--------------------------------------

                            arguments: userSnapshot[i].key.toString(),
                            );
                        },
                        
                        child: Column(
                          children: [
                            ListTile( 
                              leading: const Icon(Icons.person),
                              title: Text(userSnapshot[i].child('username').value,),
                              subtitle: Text(userSnapshot[i].child('email').value,),
                              trailing: Text(userSnapshot[i].child('phone').value.toString()),
                              onTap: (){
                                Navigator.of(context).pushNamed(
                                  UserDetailScreen.routeName,
                                  arguments: userSnapshot[i].key.toString()
                                  );
                              },
                            ),
                            const Divider(),
                          ],
                        ),
                      ),
                    ],
                  )
                  ),
              ),
            ],
          );
        }
        return const Center(child: Text('There is something wrong!'),);
      },
      );

      
  }

 
    
  }

Hope someone did the Realtime database package search before :(. Please help!

Abdallah
  • 27
  • 4

1 Answers1

0

"search" is a really broad term, so it's unclear what exactly you mean to do. But the first thing to realize is that Firebase Realtime Database has quite limited query support (documented here) and they are nowhere near SQL's LIKE clause, let alone the capabilities of a dedicated full-text search engine. If you want more advanced features than Firebase support, consider using a solution that supports those features better either in addition to Firebase, or as an alternative.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807