skip to Main Content

i have a chatting application and i want to do a search function where the user can enter into a textfield another users username and to show the searched users username and name so the user can message them,

the problem i have is that when i retrieved from my firebase the user with the same username entered it returned a Future<dynamic> instance which then results in an error in using docs: "The getter 'docs' isn't defined for the type 'Future<dynamic>' "

here is my code

class _search extends State<search> {
  TextEditingController searchController = new TextEditingController();
  late Future<dynamic> searchResult;
  bool haveUserSearched =false;

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("search for user"),
        centerTitle: true,
      ),

      body: Container(
        child: Column(
          children: [
            Container(
              color: Color(0xfffffefa),
              padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
              child: Row(
                children: [
                  // GestureDetector(
                  //   onTap: (){
                  //     print(getUserByUsername(searchController.text));
                  //   },
                  //   child: 
                    Expanded(child: TextField(
                    controller: searchController,
                    style: TextStyle(color: Color(0xffBFBBB7)),
                    onSubmitted: (value){
                      print(getUserByUsername(searchController.text));
                    },
                    decoration: InputDecoration(
                      hintText: "search by username",
                      hintStyle: TextStyle(color: Color(0xffBFBBB7)),
                      border: InputBorder.none,
                      prefixIcon: Icon(Icons.search,color: Color(0xffBFBBB7),),
                    ),
                  ),
                  ),
                  //),
                ],
              ),
            ),
          ],
        ),
      ),
    );
    
  }

  //-------methods and widgets-------
  getUserByUsername(String username) async {
    return await FirebaseFirestore.instance.collection('users').where('name',isEqualTo: username).get();
  }

  Widget userTile(String name,String username){
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
      child: Row(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                name,
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 16
                ),
              ),
              Text(
                username,
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 16
                ),
              )
            ],
          ),
          Spacer(),
          GestureDetector(
            onTap: (){
              //sendMessage(userName);
            },
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 12,vertical: 8),
              decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.circular(24)
              ),
              child: Text("Message",
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 16
                ),),
            ),
          )
        ],
      ),
    );
  }

  Widget userList(){
    return haveUserSearched ? ListView.builder(
      shrinkWrap: true,
      itemCount: 1, ///?
        itemBuilder: (context, index){
        return userTile(
          searchResult.docs[index].data['name'], //the error here is in docs "The getter 'docs' isn't defined for the type 'Future<dynamic>' "
          searchResult.docs[index].data["username"],
        );
        }) : Container();
  }
}

2

Answers


  1. searchResult is a Future, a representation of an eventual result (or error) from an asynchronous operation. You need to wait for the result, which you can do in various ways, such as await or FutureBuilder. In this circumstance, you may opt to choose the latter.

    Please see Asynchronous programming: futures, async, await for more.

    Login or Signup to reply.
  2. Use futureBuilder inside Column and pass fetched data like Widget userList(users) {

    It can be like

    class _search extends State<search> {
      TextEditingController searchController = new TextEditingController();
    
      bool haveUserSearched = false;
      late Future<dynamic> searchResult = getUserByUsername(searchController.text);
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("search for user"),
            centerTitle: true,
          ),
          body: Container(
            child: Column(
              children: [
                Container(
                  color: Color(0xfffffefa),
                  padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
                  child: Row(
                    children: [
                      Expanded(
                        child: TextField(
                          controller: searchController,
                          style: TextStyle(color: Color(0xffBFBBB7)),
                          onSubmitted: (value) {
                            searchResult = getUserByUsername(searchController.text);
                            setState(() {});
                          },
                          decoration: InputDecoration(
                            hintText: "search by username",
                            hintStyle: TextStyle(color: Color(0xffBFBBB7)),
                            border: InputBorder.none,
                            prefixIcon: Icon(
                              Icons.search,
                              color: Color(0xffBFBBB7),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                FutureBuilder(
                  future: searchResult,
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return userList(snapshot.data);
                    }
    
                    return Text("handle other state");
                  },
                ),
              ],
            ),
          ),
        );
      }
    
      //-------methods and widgets-------
      getUserByUsername(String username) async {
        final result  = await FirebaseFirestore.instance
            .collection('users')
            .where('name', isEqualTo: username)
            .get();
       User myUser  = User(name: result['name'] .....)  //get user from Map.. it cant be..
       return myUser;  
      }
    
      Widget userTile(String name, String username) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
          child: Row(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    name,
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  ),
                  Text(
                    username,
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  )
                ],
              ),
              Spacer(),
              GestureDetector(
                onTap: () {
                  //sendMessage(userName);
                },
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                  decoration: BoxDecoration(
                      color: Colors.blue, borderRadius: BorderRadius.circular(24)),
                  child: Text(
                    "Message",
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  ),
                ),
              )
            ],
          ),
        );
      }
    
      Widget userList(user) {
        return haveUserSearched
            ?  userTile(
                    user.name,
                    user.username,
                  )
            : Container();
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search