chenenyu / lifecycle

Lifecycle support for Flutter widgets.
https://pub.dev/packages/lifecycle
Apache License 2.0
50 stars 6 forks source link

log中引发的一个空指针异常 #1

Closed junixapp closed 3 years ago

junixapp commented 3 years ago

日志信息:

════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The getter 'settings' was called on null.
Receiver: null
Tried calling: settings

When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      LifecycleObserver.didRemove (package:lifecycle/src/lifecycle_observer.dart:197:41)
#2      _NavigatorRemoveObservation.notify (package:flutter/src/widgets/navigator.dart:2679:14)
#3      List.forEach (dart:core-patch/growable_array.dart:313:8)
#4      NavigatorState._flushObserverNotifications (package:flutter/src/widgets/navigator.dart:3303:27)
...
Handler: "onTap"

具体代码定位到:

/// [route] 被移除的route
  /// [previousRoute] 被移除route下面的route,移除多个route时,该参数值不变
  @override
  void didRemove(Route route, Route previousRoute) {
    super.didRemove(route, previousRoute);
    log('LifecycleObserver($hashCode)#didRemove('
        'route: ${route.settings.name}, '
        'previousRoute: ${previousRoute.settings.name})');

    _sendEventToGivenRoute(route, LifecycleEvent.pop);
    if (previousRoute.isCurrent) {
      _sendEventToGivenRoute(previousRoute, LifecycleEvent.active);
    }

    _routes.remove(route);
  }

打印previousRoute.settings.name时报错的。 我的场景是移除所有router然后pushName一个新的就报错了,使用的方法是pushNamedAndRemoveUntil; 此时previousRoute是null

flutter版本 1.22.4

我把代码修改为下面这样后就可以了:

 void didRemove(Route route, Route previousRoute) {
    super.didRemove(route, previousRoute);
    log('LifecycleObserver($hashCode)#didRemove('
        'route: ${route?.settings?.name}, '
        'previousRoute: ${previousRoute?.settings?.name})');

    _sendEventToGivenRoute(route, LifecycleEvent.pop);
    if (previousRoute?.isCurrent??false) {
      _sendEventToGivenRoute(previousRoute, LifecycleEvent.active);
    }

    _routes.remove(route);
  }
chenenyu commented 3 years ago

谢谢反馈 👍 最晚明天会发一个修复版本

junixapp commented 3 years ago

@chenenyu 0.1.1版本少加一个地方,目前还是报错:

 @override
  void didRemove(Route route, Route previousRoute) {
    super.didRemove(route, previousRoute);
    log('LifecycleObserver($hashCode)#didRemove('
        'route: ${route?.settings?.name}, '
        'previousRoute: ${previousRoute?.settings?.name})');

    _sendEventToGivenRoute(route, LifecycleEvent.pop);
    if (previousRoute.isCurrent) {
      _sendEventToGivenRoute(previousRoute, LifecycleEvent.active);
    }

    _routes.remove(route);
  }

if判断哪里也要加 ?:

if (previousRoute.isCurrent) {
  _sendEventToGivenRoute(previousRoute, LifecycleEvent.active);
}

应修改为:

if (previousRoute?.isCurrent??false) {
  _sendEventToGivenRoute(previousRoute, LifecycleEvent.active);
}

今天能更新个版本吗,有点着急。。。。

chenenyu commented 3 years ago

不好意思 才看到,现在处理下

chenenyu commented 3 years ago

try lifecycle: ^0.1.1+1