flutterdio

How to open dialog only when request is taking too long time?


I want to implement simple dialog which should only show when request is taking more than 200ms.

onTap: () {
      context.read<AuthBloc>().add(LogoutRequested());
      // if taking more than 200ms -> showDialog which is closing when request is done
      Navigator.of(context).push(MaterialPageRoute(builder: (context) => const LoginPage()));
},

I am using Dio.

There is a way to do something like this?


Solution

  • You should add something like isLoadingLong to the state of AuthBloc. This value is set to true by the bloc if the login takes longer than 200ms and is set to false when the request is completed. The dialog can then be opened and closed with a BlocListener.

    EDIT: You can set the value to true as follows:

    final timer = Timer(Duration(milliseconds: 200), () => emit(state.copyWith(isLoadingLong: true)));
    await yourRequest();
    timer.cancel();
    emit(state.copyWith(isLoadingLong: false));