Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] fix:use notifyListeners after ChangeNotifier disposed #555

Merged
merged 4 commits into from
Jul 9, 2022

Conversation

laiiihz
Copy link
Contributor

@laiiihz laiiihz commented Jun 14, 2022

when onRefresh is processing and dispose the EasyRefresh Widget will cause error. this PR fix this issue.

E/flutter (26539): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: A HeaderNotifier was used after being disposed.
E/flutter (26539): Once you have called dispose() on a HeaderNotifier, it can no longer be used.
E/flutter (26539): #0      ChangeNotifier._debugAssertNotDisposed.<anonymous closure>
package:flutter/…/foundation/change_notifier.dart:125
E/flutter (26539): #1      ChangeNotifier._debugAssertNotDisposed
package:flutter/…/foundation/change_notifier.dart:131
E/flutter (26539): #2      ChangeNotifier.notifyListeners
package:flutter/…/foundation/change_notifier.dart:304
E/flutter (26539): #3      IndicatorNotifier._setMode
package:flutter_easy_refresh/…/notifier/indicator_notifier.dart:599
E/flutter (26539): #4      IndicatorNotifier._onTask
package:flutter_easy_refresh/…/notifier/indicator_notifier.dart:544
E/flutter (26539): <asynchronous suspension>
E/flutter (26539):

set a MaterialHeader and dispose the Widget with a little bit fast speed will cause another issue. also fixed.


════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
_EasyRefreshState#40ea4(tickers: tracking 1 ticker) was disposed with an active Ticker.

_EasyRefreshState created a Ticker via its TickerProviderStateMixin, but at the time dispose() was called on the mixin, that Ticker was still active. All Tickers must be disposed before calling super.dispose().

Tickers used by AnimationControllers should be disposed by calling dispose() on the AnimationController itself. Otherwise, the ticker will leak.

The offending ticker was: _WidgetTicker(created by _EasyRefreshState#40ea4)
The stack trace when the _WidgetTicker was actually created was:
#0      new Ticker.<anonymous closure>
package:flutter/…/scheduler/ticker.dart:67
#1      new Ticker
package:flutter/…/scheduler/ticker.dart:69
#2      new _WidgetTicker
package:flutter/…/widgets/ticker_provider.dart:385
#3      TickerProviderStateMixin.createTicker
package:flutter/…/widgets/ticker_provider.dart:295
#4      new AnimationController.unbounded
package:flutter/…/animation/animation_controller.dart:279
#5      IndicatorNotifier._initClampingAnimation
package:flutter_easy_refresh/…/notifier/indicator_notifier.dart:267
#6      new IndicatorNotifier
package:flutter_easy_refresh/…/notifier/indicator_notifier.dart:44
#7      new HeaderNotifier
package:flutter_easy_refresh/…/notifier/indicator_notifier.dart:704
#8      _EasyRefreshState._initData
package:flutter_easy_refresh/src/easy_refresh.dart:317
#9      _EasyRefreshState.initState
package:flutter_easy_refresh/src/easy_refresh.dart:277
#10     StatefulElement._firstBuild
package:flutter/…/widgets/framework.dart:4942
#11     ComponentElement.mount
package:flutter/…/widgets/framework.dart:4781
...     Normal element mounting (19 frames)
  • the code.
import 'package:flutter/material.dart';
import 'package:flutter_easy_refresh/easy_refresh.dart';

class Example extends StatefulWidget {
  const Example({Key? key}) : super(key: key);

  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  int _count = 0;
  final _controller = EasyRefreshController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('DEMO')),
      body: EasyRefresh(
        // set a MaterialHeader also cause error
        header: MaterialHeader(),
        controller: _controller,
        refreshOnStart: true,
        onRefresh: () async {
          _count = 10;
          await Future.delayed(const Duration(seconds: 2));
          if (mounted) setState(() {});
        },
        child: ListView.builder(
          itemBuilder: (context, index) {
            return ListTile(title: Text('tile: $index'));
          },
          itemCount: _count,
        ),
      ),
    );
  }
}

@laiiihz laiiihz changed the title fix:use notifyListeners after ChangeNotifier disposed [v3] fix:use notifyListeners after ChangeNotifier disposed Jun 14, 2022
@xuelongqy
Copy link
Owner

Thanks, looks like a good solution. But there is a conflict in the code, if you have time to resolve it, I will happily merge

@laiiihz
Copy link
Contributor Author

laiiihz commented Jul 9, 2022

conflict resolved 🙌

@xuelongqy xuelongqy merged commit 506e75d into xuelongqy:v3 Jul 9, 2022
@xuelongqy
Copy link
Owner

nice job, merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants