user15452627
user15452627

Reputation:

Searching user by username?

Im trying to search user by username but getting an error maybe anyone can help. I wanna search like this

class MyprofilTile extends StatelessWidget {
  final myprofil profile;
  

  Future getusers()async{
    var firestore= FirebaseFirestore.instance;
   QuerySnapshot qn = await  firestore.collection('meinprofilsettings').get();
return qn.docs.where((p) => p.startsWith(query)).toList();
  }

  MyprofilTile({this.profile});
  @override
  Widget build(BuildContext context) {
    return Container(
      child:FutureBuilder(
        future:getusers(),
        builder: (_,snapshot){
        if(snapshot.connectionState ==ConnectionState.waiting){
          return  Center(
              child: Container(
              child: CircularProgressIndicator()),
            );
        }else{
          return ListView.builder(itemCount: snapshot.data.length, itemBuilder: (_,index){
          return ListTile(
          title:Text(snapshot.data[index].data()['username']),
          );
      }
      );
      

So in the Future method above this im trying to search and here I returning this whole class


class DataSearch extends SearchDelegate<String> {
  @override
  List<Widget> buildActions(BuildContext context) {
    // actions fro app bar
    return [IconButton(icon: Icon(Icons.clear), onPressed: () {query = "";

    },),];
  }

  @override
  Widget buildLeading(BuildContext context) {
    // leading icon on the left of the app bar
    return IconButton(
        icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow,
          progress: transitionAnimation,
        ),
        onPressed: () {
          close(context, null);});
  }

  @override
  Widget buildResults(BuildContext context) {
    // show some result based on the selection
    return Container();
  }

  @override
  Widget buildSuggestions(BuildContext context,{String url, username, email, uid, receiperid}) {
    // show when someone searches for something
    return MyprofilTile();
  }
}

What I want is that if user like tap S getting all users that the first letter is S and then tap Sp getting all users where first 2 letters of name contains with Sp and so on. And also react of uppercases . So it should be irellevant if user tap S or s.

The error is this : The method 'startsWith' isn't defined for the type documetnsnapshot . And also undefined name query ,

Upvotes: 0

Views: 52

Answers (1)

Fatima ayaa
Fatima ayaa

Reputation: 650

you have to search in the attribut username not in whole doc

  Future getusers()async{
    var firestore= FirebaseFirestore.instance;
   QuerySnapshot qn = await  firestore.collection('meinprofilsettings').get();
return qn.docs.where((p) => p.data()['username'].toString().startsWith(query)).toList();
  }

update your constructor

class MyprofilTile extends StatelessWidget { 
    final myprofil profile; final String query; // add this 
    MyprofilTile({this.query}`); //update this`

and in your class DataSearch

@override
  Widget buildSuggestions(BuildContext context,{String url, username, email, uid, receiperid}) {
    // show when someone searches for something
    return MyprofilTile(query: query);
  }

Upvotes: 1

Related Questions