JackJonson / flutter_styled_toast

A Styled Toast Flutter package.
Apache License 2.0
76 stars 43 forks source link

Should the BuildContext parameter in showToast really exist? #36

Open jonasborggren opened 7 months ago

jonasborggren commented 7 months ago

We've been using this library in multiple ways without knowing (I wasn't a part in setup of the project), here's what I mean:

We had an issue where the provided context could be disposed while the toast was showing (and/or was trying to dismiss), causing the following error:

Null check operator used on a null value
#0      OverlayEntry.remove (package:flutter/src/widgets/overlay.dart:170)
#1      ToastFuture.dismiss (package:flutter_styled_toast/src/styled_toast_manage.dart:74)
#2      new ToastFuture.create.<anonymous closure> (package:flutter_styled_toast/src/styled_toast_manage.dart:51)
#3      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18)
#4      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398)
#5      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429)
#6      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184)
/// in a widget under the same widget tree
/// `context` is from the widget
showToast(..., context: context, ...);
// or
showToastWidget(Text(...), context: context, ..);

At least that's the reason we think it's happening, since it stops happening when we set context parameter to null or don't use context parameter at all.

Why be able to do both?

One way this could be avoided would be to have showToast that doesn't have context parameter, and another called showToastOn with a context parameter.

Let me know if I can help.

jonasborggren commented 7 months ago

Actually, #28 is probably due to this too.

egonbeermat commented 7 months ago

@jonasborggren It is/was, and the simple modification I posted there has eliminated any further reports in Crashlytics - I could never reproduce the error locally. This package appears abandoned, so I just use locally hosted / modified source in my app now.

jonasborggren commented 7 months ago

Can you post your changes to a fork of this library perhaps? Thanks for letting me know.