fquery icon indicating copy to clipboard operation
fquery copied to clipboard

Query function is called twice for every input change

Open itsmadhusudhan opened this issue 1 year ago • 2 comments

Query function searchCustomers is being called twice when name changes once. After the first call becomes success another call is made for the same name.

Is there anything wrong I'm doing the below code?

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fquery/fquery.dart';


class SelectCustomerBottomsheet extends HookWidget {
  const SelectCustomerBottomsheet({super.key});

  Future searchCustomers(String name) async {
    if (name.length < 3) {
      return [];
    }

    return await getIt<CustomerRepository>().searchCustomers(
      name: name,
    );
  }

  @override
  Widget build(BuildContext context) {
    final name = useState("");
    final debouncedName = useDebounced(name.value, Duration(milliseconds: 300));
    final query = debouncedName ?? "";

    final result = useQuery(
      ["searchCustomers", query],
      () => searchCustomers(query),
    );
    final customers = result.data ?? [];

    return DraggableScrollableSheet(
      expand: false,
      initialChildSize: 0.9,
      maxChildSize: 0.9,
      minChildSize: 0.87,
      builder: (context, scrollController) {
        return TapOutsideGesture(
          child: Scaffold(
            body: NestedScrollView(
              headerSliverBuilder: (context, innerBoxIsScrolled) {
                return [
                  SliverAppBar(
                    title: Text('Select Customer'),
                    automaticallyImplyLeading: false,
                    scrolledUnderElevation: 0,
                    elevation: 0,
                    bottom: PreferredSize(
                      preferredSize: Size.fromHeight(50),
                      child: Container(
                        padding: const EdgeInsets.symmetric(horizontal: 20),
                        child: Input(
                          name: "name",
                          placeholderText: "Search",
                          icon: Icons.person,
                          onChanged: (value) {
                            name.value = value.trim();
                          },
                        ),
                      ),
                    ),
                  )
                ];
              },
              body: CustomScrollView(
                controller: scrollController,
                shrinkWrap: true,
                slivers: [
                  SliverPadding(
                    padding: const EdgeInsets.all(20),
                    sliver: SliverList.separated(
                      itemBuilder: (context, index) {
                        final customer = customers[index];
                        return ListTile(
                          title: Text(
                            customer.name,
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          subtitle: Text(
                            customer.phoneNumber,
                            style: TextStyle(fontSize: 14),
                          ),
                          onTap: () {
                            Navigator.pop(context, customer);
                          },
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                          tileColor: AppColors.white,
                        );
                      },
                      itemCount: customers.length,
                      separatorBuilder: (context, index) => Spacing.v20,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}

itsmadhusudhan avatar Dec 17 '24 12:12 itsmadhusudhan

Did you find out the solution for this? I'm having the same issue.

rickyoleary avatar Feb 13 '25 19:02 rickyoleary

Will look into this soon

41y08h avatar Mar 02 '25 20:03 41y08h

closing as duplicate of #39 (that has been resolved)

41y08h avatar Mar 28 '25 10:03 41y08h