1

When I try to use confirmDismiss on Dismissible widget with showAlertDialog, it pops up alert but, also throws exception and stop app. If I try to continue after exception, it works normal.

Maybe similar to this question I've tried showAlertDialog to separate function and different type of calling navigation, but non of them solve it.

            Dismissible(
              confirmDismiss: (direction) {
                return showDialog(
                  context: context,
                  builder: (context) {
                    return CupertinoAlertDialog(
                      title: Text('Delete'),
                      content: Text('Delete'),
                      actions: <Widget>[
                        FlatButton(
                          onPressed: () {
                            // Navigator.pop(context, false);
                            Navigator.of(
                              context,
                              // rootNavigator: true,
                            ).pop(false);
                          },
                          child: Text('No'),
                        ),
                        FlatButton(
                          onPressed: () {
                            // Navigator.pop(context, true);
                            Navigator.of(
                              context,
                              // rootNavigator: true,
                            ).pop(true);
                          },
                          child: Text('Yes'),
                        ),
                      ],
                    );
                  },
                );
              },
              key: Key(UniqueKey().toString()),
              direction: DismissDirection.endToStart,
              onDismissed: (direction) {
                //TODO DELETE
                Scaffold.of(context).showSnackBar(
                  SnackBar(
                    backgroundColor: Theme.of(context).accentColor,
                    content: Text(
                      'test',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 20.0,
                        color: Colors.white,
                      ),
                    ),
                  ),
                );
              },
              background: Container(
                padding: const EdgeInsets.only(right: 20.0),
                // alignment: AlignmentDirectional.centerEnd,
                alignment: Alignment.centerRight,
                color: Theme.of(context).accentColor,
                child: Icon(
                  Icons.delete,
                  size: 32.0,
                  color: Theme.of(context).primaryColor,
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 20.0),
                child: CartCard(),
              ),
            ),

this is exception

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: 'package:flutter/src/animation/animation_controller.dart': Failed assertion: line 484 pos 7: '_ticker != null': AnimationController.reverse() called after AnimationController.dispose()
AnimationController methods should not be used after calling dispose.
#0      _AssertionError._doThrowNew  (dart:core-patch/errors_patch.dart:40:39)
#1      _AssertionError._throwNew  (dart:core-patch/errors_patch.dart:36:5)
#2      AnimationController.reverse 
package:flutter/…/animation/animation_controller.dart:484
#3      _DismissibleState._handleDismissStatusChanged 
package:flutter/…/widgets/dismissible.dart:449
<asynchronous suspension>
#4      AnimationLocalStatusListenersMixin.notifyStatusListeners 
package:flutter/…/animation/listener_helpers.dart:193
#5      AnimationController._checkStatusChanged 
package:flutter/…/animation/animation_controller.dart:753
#6      AnimationController._tick (package:flutter/src/animation/animation_contr<…>
user12208004
  • 1,704
  • 3
  • 15
  • 37
  • I didn't confirm with the same code yet, but this is related to `CupertinoTabView`, and using `key: ValueKey(yourItem),` could be solve this problem. I got from [here](https://github.com/flutter/flutter/issues/46766#issuecomment-565629675) – user12208004 Feb 11 '20 at 11:49

2 Answers2

2

Your problem is not with the dismiss or confirm dismiss method. The error warning is clear: Your issue is with the Animation Controller.

Apparently, somewhere in your code, you are disposing of it - most likely in the:

 @override dispose 

Try to remove it and see if it works.

OK try this

        Dismissible(
          confirmDismiss: (direction) {
            return showDialog(
              context: context,
              builder: (context) {
                return CupertinoAlertDialog(
                  title: Text('Delete'),
                  content: Text('Delete'),
                  actions: <Widget>[
                    FlatButton(
                      onPressed: () {
                        // Navigator.pop(context, false);
                        Navigator.of(
                          context,
                          // rootNavigator: true,
                        ).pop(false);
                      },
                      child: Text('No'),
                    ),
                    FlatButton(
                      onPressed: () {
                        // Navigator.pop(context, true);
                        Navigator.of(
                          context,
                          // rootNavigator: true,
                        ).pop(true);
                      },
                      child: Text('Yes'),
                    ),
                  ],
                );
              },
            );
          },
          key: UniqueKey(),
          direction: DismissDirection.endToStart,
          onDismissed: (direction) {
            //TODO DELETE
            Scaffold.of(context).showSnackBar(
              SnackBar(
                backgroundColor: Theme.of(context).accentColor,
                content: Text(
                  'test',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
              ),
            );
          },
          background: Container(
            padding: const EdgeInsets.only(right: 20.0),
            // alignment: AlignmentDirectional.centerEnd,
            alignment: Alignment.centerRight,
            color: Theme.of(context).accentColor,
            child: Icon(
              Icons.delete,
              size: 32.0,
              color: Theme.of(context).primaryColor,
            ),
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20.0),
            child: CartCard(),
          ),
        ),
houba
  • 496
  • 7
  • 20
  • Thanks for your answer, but I did't use any dispose method. I'm not sure but, I think this animation controller is connected to dismiss widget, and not my own animation – user12208004 Oct 16 '19 at 06:13
  • I update my comment, but you agree that you have an animation controller somewhere in there?? right? – houba Oct 16 '19 at 21:31
  • Thanks for your comment, but it doesn't work. Sorry my explanation was bad, I mean dismiss widget itself contains the animation controller, but I did not add any additional controller. – user12208004 Oct 17 '19 at 06:25
  • I don't see any reference to an animation controller in this dismiss routine. Have you pasted everything? – houba Oct 17 '19 at 16:15
  • I did it. DismissibleState contains an animation controller to control dismiss gesture and its animation. – user12208004 Oct 18 '19 at 06:20
1

all you need to do is write a function for example

Future<bool> sample(DismissDirection direction) async{
   #write you code
   return true or false;
}

it's work for me