Skip to content

Crash when toggling loading state immediately from initState #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rockgecko-development opened this issue Oct 14, 2021 · 3 comments · May be fixed by #2
Closed

Crash when toggling loading state immediately from initState #1

rockgecko-development opened this issue Oct 14, 2021 · 3 comments · May be fixed by #2

Comments

@rockgecko-development
Copy link

Hi, thanks for the library!

If I have no await delay in between setting _loading to true and false, the component remains showing the loading spinner indefinitely. If I have a very short delay instead (eg 1-10 ms), the component crashes with:


======== Exception caught by widgets library =======================================================
The following assertion was thrown building _BodyBuilder:
Hide called, but not showing!
'package:declarative_refresh_indicator/declarative_refresh_indicator.dart':
Failed assertion: line 122 pos 12: '_showing'

Example:

@override
  void initState() {
    super.initState();
    _refresh();
  }
 void _refresh() async {
    setState(() => _loading = true);
    //wait anything under ~500ms, or have no `await` at all
    await Future<void>.delayed(Duration(milliseconds: 1));
    if (mounted) setState(() => _loading = false);
  }

This occurs if _refresh is called immediately from initState.

@hacker1024
Copy link
Owner

Sorry for the delay, I'll look into your PR in the next two weeks or so. Thanks!

@hacker1024
Copy link
Owner

Hi @rockgecko-development,
I apologise again for the wait - I've been pretty busy.

The problem here, as you may have identified, is that setState is being called before the internal post-frame callback that initialises the widget is executed. This results in a race condition where didUpdateWidget is being called with refreshing = false before the post-frame callback that shows the indicator does its job.

setState should not be called in initState. You can call _refresh for the first time in a post-frame callback as well. I will fix this in the example.

Is this still an issue when setState is not called in initState?

@hacker1024
Copy link
Owner

Closing as this issue appears not to occur when initState is used correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants