dart - 滚动 Controller flutter 错误发生异常

标签 dart flutter

我有两个屏幕,第一个从 wordpress 获取所有帖子
最后一个获取所有视频,我使用带有 ListView 的 ScollController 滚动数据

看看我的代码:

ScrollController _scrollController;

@override
  void initState() {
    _scrollController = new ScrollController();
    _scrollController.addListener(() {
      print(_scrollController.position.pixels);
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent - 10) {
        // if we are the bottom of the page
        fetchPosts();
        //millisecs += 1500;
        //super.initState();
      }
      super.initState();
    });
  }

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

  @override
  Widget build(BuildContext context) { ....

当我在屏幕一到两个或屏幕二到一之间切换时

我收到此错误:
    Exception has occurred.
FlutterError (A ScrollController was used after being disposed.
Once you have called dispose() on a ScrollController, it can no longer be used.)

跟踪代码:
image error

Launching lib\main.dart on Android SDK built for x86 in debug mode... Built build\app\outputs\apk\debug\app-debug.apk. D/EGL_emulation( 8157): eglMakeCurrent: 0xeeebd340: ver 3 1 (tinfo 0xeee9b1d0) E/eglCodecCommon( 8157): glUtilsParamSize: unknow param 0x000082da E/eglCodecCommon( 8157): glUtilsParamSize: unknow param 0x000082da I/Choreographer( 8157): Skipped 49 frames! The application may be doing too much work on its main thread. D/EGL_emulation( 8157): eglMakeCurrent: 0xeef21ce0: ver 3 1 (tinfo 0xe6f286b0) D/ ( 8157): HostConnection::get() New Host Connection established 0xe7123880, tid 8184 D/EGL_emulation( 8157): eglMakeCurrent: 0xeeebd340: ver 3 1 (tinfo 0xe7103370) I/flutter ( 8157): _morePages: false I/flutter ( 8157): statusCode: 200 I/flutter ( 8157): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════ I/flutter ( 8157): The following assertion was thrown while dispatching notifications for ScrollController: I/flutter ( 8157): 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: I/flutter ( 8157): '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): I/flutter ( 8157): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter ( 8157): more information in this error message to help you determine and fix the underlying cause. I/flutter ( 8157): In either case, please report this assertion by filing a bug on GitHub: I/flutter ( 8157): https://github.com/flutter/flutter/issues/new?template=BUG.md I/flutter ( 8157): I/flutter ( 8157): When the exception was thrown, this was the stack: I/flutter ( 8157): #2 State.initState (package:flutter/src/widgets/framework.dart:1003:12) I/flutter ( 8157): #3 _PostsPage.initState. (package:flutter_wp2019/ui/PostsLatest.dart:42:13) I/flutter ( 8157): #4 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:208:21) I/flutter ( 8157): #5 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:208:21) I/flutter ( 8157): #6 ScrollPosition.notifyListeners (package:flutter/src/widgets/scroll_position.dart:692:11) I/flutter ( 8157): #7 ScrollPosition.setPixels (package:flutter/src/widgets/scroll_position.dart:218:9) I/flutter ( 8157): #8 ScrollPositionWithSingleContext.setPixels (package:flutter/src/widgets/scroll_position_with_single_context.dart:84:18) I/flutter ( 8157): #9 ScrollPositionWithSingleContext.applyUserOffset (package:flutter/src/widgets/scroll_position_with_single_context.dart:127:5) I/flutter ( 8157): #10 ScrollDragController.update (package:flutter/src/widgets/scroll_activity.dart:373:14) I/flutter ( 8157): #11 ScrollableState._handleDragUpdate (package:flutter/src/widgets/scrollable.dart:470:12) I/flutter ( 8157): #12 DragGestureRecognizer.acceptGesture. (package:flutter/src/gestures/monodrag.dart:176:48) I/flutter ( 8157): #13 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24) I/flutter ( 8157): #14 DragGestureRecognizer.acceptGesture (package:flutter/src/gestures/monodrag.dart:176:9) I/flutter ( 8157): #15 GestureArenaManager._resolveInFavorOf (package:flutter/src/gestures/arena.dart:263:12) I/flutter ( 8157): #16 GestureArenaManager._resolve (package:flutter/src/gestures/arena.dart:222:9) I/flutter ( 8157): #17 GestureArenaEntry.resolve (package:flutter/src/gestures/arena.dart:52:12) I/flutter ( 8157): #18 OneSequenceGestureRecognizer.resolve (package:flutter/src/gestures/recognizer.dart:166:13) I/flutter ( 8157): #19 DragGestureRecognizer.handleEvent (package:flutter/src/gestures/monodrag.dart:154:11) I/flutter ( 8157): #20 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12) I/flutter ( 8157): #21 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11) I/flutter ( 8157): #22 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:180:19) I/flutter ( 8157): #23 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22) I/flutter ( 8157): #24 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7) I/flutter ( 8157): #25 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7) I/flutter ( 8157): #26 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7) I/flutter ( 8157): #27 _invoke1 (dart:ui/hooks.dart:168:13) I/flutter ( 8157): #28 _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5) I/flutter ( 8157): (elided 2 frames from class _AssertionError) I/flutter ( 8157): I/flutter ( 8157): The ScrollController sending notification was: I/flutter ( 8157): ScrollController#bcc47(one client, offset 21.4) I/flutter ( 8157): ════════════════════════════════════════════════════════════════════════════════════════════════════ I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/chatty ( 8157): uid=10083(com.example.flutterwp2019) 1.ui identical 16 lines I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/zygote ( 8157): Do partial code cache collection, code=29KB, data=23KB I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/chatty ( 8157): uid=10083(com.example.flutterwp2019) 1.ui identical 1 line I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/zygote ( 8157): After code cache collection, code=29KB, data=23KB I/zygote ( 8157): Increasing code cache capacity to 128KB I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/chatty ( 8157): uid=10083(com.example.flutterwp2019) 1.ui identical 11 lines I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/zygote ( 8157): Do partial code cache collection, code=59KB, data=40KB I/zygote ( 8157): After code cache collection, code=59KB, data=40KB I/zygote ( 8157): Increasing code cache capacity to 256KB I/flutter ( 8157): 30.00837053571422 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 42.86551339285711 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 55.72265625 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 71.41741071428567 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 88.56863839285711 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 105.71986607142856 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 120.24285714285713 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 117.38013392857141 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 110.24843749999997 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 93.09720982142858 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 63.0888392857143 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 23.11116071428569 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 0.0 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 1.4313616071428896 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 8.58816964285711 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 24.006696428571445 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 41.15792410714289 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 64.00948660714289 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 85.4296875 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 108.28125 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 120.24285714285713 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 2.8376116071428896 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 7.131696428571445 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 11.42578125 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 18.557477678571445 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 24.28292410714289 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 39.977678571428555 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 57.12890625 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 77.14285714285717 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 97.13169642857144 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 115.71428571428572 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): 120.24285714285713 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): _morePages: false I/flutter ( 8157): statusCode: 200 I/flutter ( 8157): _morePages: false I/flutter ( 8157): statusCode: 400 I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/chatty ( 8157): uid=10083(com.example.flutterwp2019) 1.ui identical 22 lines I/flutter ( 8157): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 1003 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true. I/flutter ( 8157): _morePages: false I/flutter ( 8157): statusCode: 200



image error

感谢提前

最佳答案

super.initState()在错误的位置

@override
  void initState() {
    super.initState(); // <<< added

    _scrollController = new ScrollController();
    _scrollController.addListener(() {
      print(_scrollController.position.pixels);
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent - 10) {
        // if we are the bottom of the page
        fetchPosts();
        //millisecs += 1500;
        //super.initState();
      }
      // super.initState(); // <<< removed
    });

  }

关于dart - 滚动 Controller flutter 错误发生异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54184326/

相关文章:

flutter - 如何在Flutter中清除ListViewBuilder中的对象?

flutter - 当此属性更改时,如何对显示对象属性的文本小部件进行 react 性更新?这与 GetX

当 sqflite 数据库数据更改时,Flutter 提供程序监听器不会自行更新

flutter - 如何热重载状态字段?

dart - Dart 中关于类成员的封装和类型注释的约定是什么?

dart - 如何从 dart 中的函数返回 Future 或任何其他类型

dart - 流函数调用在Google Dart中是异步的吗?

Flutter 热重载不适用于 VSCode 中的自动保存

Flutter:自定义底部导航栏切割半径

dart - Flutter invertColor 不会反转手机中的颜色