0

I am using graphql_flutter: ^5.1.0 from pub.dev. Its very easy and helpful. I am able to make queries and mutations easily in it. The only problem is now I am in a situation where I am supposed to call a search query again and again everytime the user enters something in search box. The way I am trying is I have created a separate query method for query widget. In that I am passing different variable value. But it doesnt work. And it gives me same result as before. I am just not sure how I make fresh query call with fresh variable values again and again.

My current code:

      @override
      Widget build(BuildContext context) {
        nameSearch = NameSearch(edgesList, (String s) {
          print("searched letter $s");
    
          if (s != null && !s.isEmpty) {
            allFilmsQuery(value: ++count);
          }
        });
    
        return Container(
          child: GraphQLProvider(
              client: GraphqlConfig.initializeClient(),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                      onPressed: () async {
                        final result = await showSearch<Edges>(
                          context: context,
                          delegate: nameSearch,
                        );
    
                        print("searched result $result");
                      },
                      child: Text("Search")), 
                      allPeopleQuery()
                ],
              )),
        );
      }

Query method that I have created:

Query allPeopleQuery({int value = 1}) {
    var map = MyQueries.getMap(value);

    print("allFilmsQuery called:$map");

    return Query(
      options: QueryOptions(
        fetchPolicy: FetchPolicy.networkOnly,
        document: gql(MyQueries.allPeople),
        // this is the query string you just created
        variables: map,
        pollInterval: const Duration(seconds: 10),
      ),
      builder: (QueryResult result,
          {VoidCallback? refetch, FetchMore? fetchMore}) {
        if (result.hasException) {
          return Text(result.exception.toString());
        }

        if (result.isLoading) {
          return const Center(child: CupertinoActivityIndicator());
        }

        var response = result.data;
        if (response != null) {
          print("allpeople result:$count ${result.data}");

          AllPeopleResponse allPeopleResponse = AllPeopleResponse.fromJson(
              response["allPeople"]);
          edgesList = allPeopleResponse.edges!;

          if (nameSearch != null) {
            nameSearch.updateList(edgesList);
          }

          return Center(child: Text(result.data.toString()));
        }

        return const Center(child: CupertinoActivityIndicator());
      },
    );
  }

As you can see the query method is being called once in beginning in build method. And it works perfectly fine. And gives me the result that I need the first time. But on recalling it on every search letter entered its not working. And keeps giving the same result it returned the first time. Can anyone help me on this, how can I make repeated graphql query calls with fresh variable values and get fresh/update results?

Cassius
  • 353
  • 3
  • 16

0 Answers0