JeffG05 / emoji_picker

A Flutter package that provides an Emoji Keyboard widget
https://pub.dev/packages/emoji_picker
BSD 2-Clause "Simplified" License
84 stars 91 forks source link

Unhandled Exception: setState() called after dispose(): _EmojiPickerState #22

Closed yokoboko closed 3 years ago

yokoboko commented 4 years ago

Hi, want to report a bug.

How to reproduce: Open a page with EmojiPicker in it and go back(pop) to previous one before emoji picker loads.

E/flutter (15896): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] #Unhandled Exception: setState() called after dispose(): _EmojiPickerState#02fa2(lifecycle state: defunct, not mounted)
E/flutter (15896): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (15896): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (15896): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter (15896): #0      State.setState.<anonymous closure>
E/flutter (15896): #1      State.setState
E/flutter (15896): #2      _EmojiPickerState.updateEmojis.<anonymous closure>
E/flutter (15896): #3      _rootRunUnary  (dart:async/zone.dart:1141:38)
E/flutter (15896): #4      _CustomZone.runUnary  (dart:async/zone.dart:1034:19)
E/flutter (15896): #5      _FutureListener.handleValue  (dart:async/future_impl.dart:140:18)
E/flutter (15896): #6      Future._propagateToListeners.handleValueCallback  (dart:async/future_impl.dart:681:45)
E/flutter (15896): #7      Future._propagateToListeners  (dart:async/future_impl.dart:710:32)
E/flutter (15896): #8      Future._completeWithValue  (dart:async/future_impl.dart:525:5)
E/flutter (15896): #9      _AsyncAwaitCompleter.complete  (dart:async-patch/async_patch.dart:34:15)
E/flutter (15896): #10     _completeOnAsyncReturn  (dart:async-patch/async_patch.dart:294:13)
E/flutter (15896): #11     _EmojiPickerState.getRecentEmojis (package:emoji_picker/emoji_picker.dart)
E/flutter (15896): <asynchronous suspension>
E/flutter (15896): #12     _EmojiPickerState.updateEmojis
E/flutter (15896): <asynchronous suspension>
E/flutter (15896): #13     _EmojiPickerState.initState
E/flutter (15896): #14     StatefulElement._firstBuild
E/flutter (15896): #15     ComponentElement.mount
E/flutter (15896): #16     Element.inflateWidget
E/flutter (15896): #17     MultiChildRenderObjectElement.mount
E/flutter (15896): #18     Element.inflateWidget
E/flutter (15896): #19     Element.updateChild
E/flutter (15896): #20     ComponentElement.performRebuild
E/flutter (15896): #21     Element.rebuild
E/flutter (15896): #22     ComponentElement._firstBuild
E/flutter (15896): #23     ComponentElement.mount
E/flutter (15896): #24     Element.inflateWidget
E/flutter (15896): #25     Element.updateChild
E/flutter (15896): #26     ComponentElement.performRebuild
E/flutter (15896): #27     Element.rebuild
E/flutter (15896): #28     ComponentElement._firstBuild
E/flutter (15896): #29     ComponentElement.mount
E/flutter (15896): #30     Element.inflateWidget
E/flutter (15896): #31     Element.updateChild
E/flutter (15896): #32     ComponentElement.performRebuild
E/flutter (15896): #33     Element.rebuild
E/flutter (15896): #34     ComponentElement._firstBuild
E/flutter (15896): #35     ComponentElement.mount
E/flutter (15896): #36     Element.inflateWidget (

Example code:

import 'package:flutter/material.dart';
import 'package:emoji_picker/emoji_picker.dart';

void main() => runApp(MainApp());

class MainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Flutter Emoji Picker Example",
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter Emoji Picker Home"),
      ),
      body: Center(
        child: FlatButton(
          color: Colors.greenAccent,
          child: Text('Show emoji screen'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => EmojiPage()),
            );
          },
        ),
      ),
    );
  }
}

class EmojiPage extends StatefulWidget {
  @override
  EmojiPageState createState() => new EmojiPageState();
}

class EmojiPageState extends State<EmojiPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter Emoji Picker Example"),
      ),
      body: Column(
        children: <Widget>[
          Container(
            width: double.maxFinite,
            height: 240,
            color: Colors.greenAccent,
            child: FlatButton(
              child: Text('BACK'),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ),
          EmojiPicker(
            rows: 3,
            columns: 7,
            recommendKeywords: ["racing", "horse"],
            numRecommended: 10,
            onEmojiSelected: (emoji, category) {
              print(emoji);
            },
          ),
        ],
      ),
    );
  }
}
bazinga-web commented 4 years ago

Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.---- That's works for me.

yokoboko commented 4 years ago

@bazinga-web I'm not calling setState(), the emoji_picker does. I think this should be fixed.

paulkastel commented 3 years ago

Yes, this is happening and is annoying!

yokoboko commented 3 years ago

Yes, this is happening and is annoying!

Are you sure that you are using the latest version? That has been fixed.

paulkastel commented 3 years ago

@yokoboko yes, version 0.1.0 from pub.dev