0

I have a ListView of items in my app and each item of that list is wrapped in a Dismissible widget so as to enable swipe to delete functionality. I also have a FloatingActionButton and on pressing that I navigate to a new page. All the functionality involving swipe to dismiss works fine but if I press the floating action button after dismissing an item, an exception is thrown with the following message:

I/flutter (23062): A dismissed Dismissible widget is still part of the tree.
I/flutter (23062): Make sure to implement the onDismissed handler and to immediately remove the Dismissible
I/flutter (23062): widget from the application once that handler has fired.

Here's the code:

Widget build(BuildContext context) {
    final String testVal = widget.testStr;
    return Scaffold(
        appBar: AppBar(
          title: Text('My Device Info'),
        ),
        body: FutureBuilder<AndroidDeviceInfo>(
          future: _deviceInfoPlugin.androidInfo,
          builder: (BuildContext context, AsyncSnapshot<AndroidDeviceInfo> snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(),
              );
            } else {
              final androidDeviceInfo = snapshot.data;

              return ListView(
                children: <Widget>[
                  Dismissible(
                    key: Key('1'),
                    background: Container(color: Colors.red,),
                    onDismissed: (direction){

                    },
                    child: ListTile(
                      leading: Icon(Icons.info),
                      title: Text('Android build version: ${androidDeviceInfo.version.release}',
                        style: TextStyle(fontSize: 18.0),),
                    ),
                  ),

                  ListTile(
                      leading: Icon(Icons.info),
                      title: Text('Android device: ${androidDeviceInfo.device}',
                        style: TextStyle(fontSize: 18.0),),
                  ),
                  ListTile(
                      leading: Icon(Icons.info),
                      title: Text('Android device hardware: ${androidDeviceInfo.hardware}', style: TextStyle(fontSize: 18.0),),
                    )
                ],
              );
            }
          },
        ),
      floatingActionButton: FloatingActionButton(
          onPressed: (){
            Navigator.of(context).push(MaterialPageRoute(builder: (context){ return new AboutPage();}));
          },
        child: Icon(Icons.add),
      ),
    );
  }

How can I avoid this exception from being thrown? Am I doing something wrong here?

1 Answer 1

0

The way you initialize the Key to your Dismissable is wrong because Flutter thinks there can be more Dismissable widgets with the same key value.

Add uuid: ^1.0.3 to your dependencies,

Dismissible(
  key: Key(Uuid().v4().toString()), //use the uuid.
    .
    .
    .
Sign up to request clarification or add additional context in comments.

2 Comments

thanks @westdabestdb it worked with uuid. although I wonder why this has to be the case. I was thinking as long as the Key is unique in the current application this should work. All the examples I've seen so far suggest some unique string within the app, not necessarily UUID. Any idea why this is the case?
Dismissable is usually being used with arrays&lists and when you dismiss one item, you also have to remove it from the array and since the array is updated, Flutter recognizes the updated list view. But in your case, you only have one dismissable and your listview children widgets are added manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.