jb3rndt / PersistentBottomNavBarV2

A highly customizable persistent bottom navigation bar for Flutter
https://pub.dev/packages/persistent_bottom_nav_bar_v2
BSD 3-Clause "New" or "Revised" License
47 stars 48 forks source link

Fix 'Null check operator used on a null value' #161

Closed lukehutch closed 1 month ago

lukehutch commented 1 month ago

Gets rid of unchecked use of !...

The exception can be triggered on a hot reload, or probably in other situations too.

jb3rndt commented 1 month ago

Thank you. I am interested in which exact cases this problem occurs because this should never happen. Could you provide an example?

lukehutch commented 1 month ago

As I mentioned hot reload triggered this for me. However, I think the issue was also triggered when I was trying to dynamically change the number of tabs (discussed in another issue).

The general rule here though is that you can never assume that a Key's currentState is non-null, since that state can be torn down anytime the widget is disposed. In particular though, you can't assume that currentState is going to be available if an onTap handler or similar is interrupted by an awaited async call.

Similarly, you should never call setState unless you first check if the widget is still mounted, if you know that there is any chance there is an await somewhere between an event handler call and the setState call.

When developing libraries, you can't assume everyone is going to call your methods in the same orthodox way you would call them yourself, so you have to protect against this sort of issue...

jb3rndt commented 1 month ago

When developing libraries, you can't assume everyone is going to call your methods in the same orthodox way you would call them yourself, so you have to protect against this sort of issue...

Sure, thats why I try to understand that "unintended" way of using the lib because that might have effects on other parts too. But I agree this can have many reasons in this case so an example might not be very useful...

Thanks for catching this!