最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Flutter: How to Refresh a ListView After Submitting Data on a Second Screen? - Stack Overflow

programmeradmin4浏览0评论

I have a ListView in my first screen that displays a list of addresses. When the user navigates to the second screen to add or edit an address and then comes back to the first screen, I want the ListView to refresh and display the updated data.

I am using Bloc for state management, and after navigating back from the second screen, I am calling an event to fetch the updated data. However, the ListView is not refreshing, even though the state is being updated correctly.

Here's my navigate() function in the first screen:

Future<void> navigate() async {
  final result = await Navigator.pushNamed(
          context,
          AppRoutes.newaddress,
          arguments: {},
);
    
      if (result == 'success') {
        // Triggering a refresh
        context.read<DashboardBloc>().add(FetchDashboard());
        Utils.showToast(context, "Address list refreshed.");
      }
    }

And here's the BlocBuilder implementation for the ListView:

BlocBuilder<DashboardBloc, DashboardState>(
      builder: (context, state) {
        if (state is DashboardLoading) {
          return Center(child: CircularProgressIndicator());
        } else if (state is DashboardLoaded) {
      final addressList = state.dashboardResponse.data.user.billingAddress.data;
    
          return ListView.builder(
            key: ValueKey(state.dashboardResponse),
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: addressList.length,
            itemBuilder: (context, index) {
              final address = addressList[index];
              return ListTile(
                title: Text(address.firstName),
                subtitle: Text(address.city.name),
              );
            },
          );
        } else if (state is AddressError) {
          return Center(child: Text("No data available"));
        } else {
          return Container();
        }
      },
    )

My Second screen Code

if (response['status'] == 'SUCCESS') {
      Utils.hideLoadingIndicator(context);
      Utils.showToast(context, response["message"]); // Show success message
      //Future.delayed(Duration(seconds: 1));
      Navigator.pop(context, 'success');
    } else {
      Utils.hideLoadingIndicator(context);
      Utils.showToast(context, response["message"]); // Show error message
    }

The FetchDashboard event is correctly called, and the state is updated as expected, but the ListView does not refresh to reflect the new data.

What could be causing this issue, and how can I ensure that the ListView refreshes with the updated data after returning from the second screen?

I have a ListView in my first screen that displays a list of addresses. When the user navigates to the second screen to add or edit an address and then comes back to the first screen, I want the ListView to refresh and display the updated data.

I am using Bloc for state management, and after navigating back from the second screen, I am calling an event to fetch the updated data. However, the ListView is not refreshing, even though the state is being updated correctly.

Here's my navigate() function in the first screen:

Future<void> navigate() async {
  final result = await Navigator.pushNamed(
          context,
          AppRoutes.newaddress,
          arguments: {},
);
    
      if (result == 'success') {
        // Triggering a refresh
        context.read<DashboardBloc>().add(FetchDashboard());
        Utils.showToast(context, "Address list refreshed.");
      }
    }

And here's the BlocBuilder implementation for the ListView:

BlocBuilder<DashboardBloc, DashboardState>(
      builder: (context, state) {
        if (state is DashboardLoading) {
          return Center(child: CircularProgressIndicator());
        } else if (state is DashboardLoaded) {
      final addressList = state.dashboardResponse.data.user.billingAddress.data;
    
          return ListView.builder(
            key: ValueKey(state.dashboardResponse),
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: addressList.length,
            itemBuilder: (context, index) {
              final address = addressList[index];
              return ListTile(
                title: Text(address.firstName),
                subtitle: Text(address.city.name),
              );
            },
          );
        } else if (state is AddressError) {
          return Center(child: Text("No data available"));
        } else {
          return Container();
        }
      },
    )

My Second screen Code

if (response['status'] == 'SUCCESS') {
      Utils.hideLoadingIndicator(context);
      Utils.showToast(context, response["message"]); // Show success message
      //Future.delayed(Duration(seconds: 1));
      Navigator.pop(context, 'success');
    } else {
      Utils.hideLoadingIndicator(context);
      Utils.showToast(context, response["message"]); // Show error message
    }

The FetchDashboard event is correctly called, and the state is updated as expected, but the ListView does not refresh to reflect the new data.

What could be causing this issue, and how can I ensure that the ListView refreshes with the updated data after returning from the second screen?

Share Improve this question edited Mar 21 at 15:10 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Mar 21 at 5:14 surya nadimintisurya nadiminti 416 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

you could rebuild the the DashboradBloc after you return success on your second screen like this:

     if (response['status'] == 'SUCCESS') {
        Utils.hideLoadingIndicator(context);
        Utils.showToast(context, response["message"]); // Show success message
        //rebuild here
        BlocProvider.of<DashboardBloc>(context).add(FetchDashboard()); //<--this code
        Navigator.pop(context, 'success');
      } else {
        Utils.hideLoadingIndicator(context);
        Utils.showToast(context, response["message"]); // Show error message
      }
发布评论

评论列表(0)

  1. 暂无评论