Demir
Demir

Reputation: 859

Flutter RefreshIndicator method is being called but the context is not rebuild

When I delete the item from the list, then I go back and refresh the page, RefreshIndicator seems not working(The animation is working but not refreshing the page). I have searched a lot about this problem. I tried everything I found on the web but none of them worked for me. The problem is that I have the method of _refresh to call this method onRefresh but it didn't work. I debugged the code to see whether the refresh method is being called. As far as I see it seems it is being called because I see refresh method is called on the debug console. The ListView.builder also has the physics property and it's not shrunk. I saw one more solution that suggests adding items that fill the whole screen. I added as many items as I can but it didn't work. So any suggestions? I am suspecting from the FutureBuilder that is a parent of the ListView.builder, I tried to cover the FutureBuilder too but it didn't work either.

class _DraftsState extends State<Drafts> {
  final SQFLiteHelper _helper = SQFLiteHelper.instance;

  @override
  void initState() {
    print('init state is called');
    super.initState();
    _helper.getForms();
  }

  Future<void> _refresh() async {
    print('refresh method is called');
    await _helper.getForms();
  }

//TODO: RefreshIndicator not working.
//TODO:When the list changed nothing is happening until the draft section is rebuilt
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<FormData>?>(
          future: _helper.getForms(),
          builder:
              (BuildContext context, AsyncSnapshot<List<FormData>?> snapshot) {
            if (snapshot.hasData && snapshot.data!.isEmpty) {
              return const Center(
                  child: Text("Henüz kaydedilmiş taslak bulunmamaktadır."));
            }
            if (snapshot.hasError) {
              return Center(
                  child: Text(
                'Bir şeyler ters gitti.',
                style: TEXT_STYLE,
              ));
            }
            if (snapshot.connectionState == ConnectionState.done) {
              return RefreshIndicator(
                backgroundColor: Colors.grey[700],
                color: LIGHT_BUTTON_COLOR,
                onRefresh: _refresh,
                child: SizedBox(
                  height: MediaQuery.of(context).size.height,
                  child: ListView.builder(
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemCount: snapshot.data!.length,
                    itemBuilder: (BuildContext context, int index) {
                      return CustomListTile(
                          formData: snapshot.data![index], index: index);
                    },
                  ),
                ),
              );
            }
            return const Center(
              child: CircularProgressIndicator(),
            );
          }),
    );
  }
}

Upvotes: 0

Views: 853

Answers (1)

Alwayss Bijoy
Alwayss Bijoy

Reputation: 844

Future<void> _refresh() async {
    print('refresh method is called');
    setState(() {
    await _helper.getForms();
    });
  }

use setState in your refresh function. coz you need to reload the build method. or I think you can use setState like this.

Future<void> _refresh() async {
    print('refresh method is called');
    await _helper.getForms();
    setState(() { });
  }

Upvotes: 1

Related Questions