当前位置: 首页 > 知识库问答 >
问题:

当使用Navigator时,我如何解析方法“祖先StateOfType”在null上被调用?

夹谷烨赫
2023-03-14

我正在使用firebase_dynamic_links:^0.5.0在flutter应用程序中实现设置密码功能。当管理员创建用户时,用户会收到一封电子邮件。在这封邮件中有一个动态链接。点击后,用户将被重定向到1。应用商店/播放商店,如果他/她没有应用。2.如果应用程序已经安装,则打开应用程序中的设置密码屏幕。firebase_dynamic_links提供了两种方法:-1.getInitiallInka future检索打开应用程序的链接。2.onlink一个回调来监听当应用程序处于活动状态或后台时打开的链接。

当应用程序处于kill状态并且链接被按下时,一切都很顺利,但是当应用程序在后台时,导航就不工作了。

void main() => runApp(MaterialApp(
    theme: new ThemeData(
      primaryColor: Colours.appThemeColour,
    ),
    onGenerateRoute: (RouteSettings settings) {
      switch (settings.name) {
        case '/': return new MyCustomRoute(
          builder: (_) => new SplashScreen(),
          settings: settings,
        );
        case '/setpassword': return new MyCustomRoute(
          builder: (_) => new SetPassword(settings.arguments),
          settings: settings,
        );
     ...


      }
    }
)
);

class SplashScreen extends StatefulWidget {

  @override
  _SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
Uri deepLink;


  _handleDeepLink(Uri deepLink) async{
    if (deepLink != null) {
      print("querParam=${deepLink.queryParameters["token"]}");
      if(deepLink.queryParameters["otp"].toString()=="1"){
        if(deepLink.queryParameters["token"]!=null){
          String _status =await _networkUtil.verifyUser(deepLink.queryParameters["token"].toString());
            Navigator.of(context).pushReplacementNamed('/setpassword',arguments: _status );


        }else{
          Navigator.of(context).pushReplacementNamed('/setpassword',arguments: "unknownError" );
        }
      }
      else{
        if(deepLink.queryParameters["token"]!=null){
          String _status =await _networkUtil.verifyUser(deepLink.queryParameters["token"].toString());
          SchedulerBinding.instance.addPostFrameCallback((_) {
            Navigator.of(context).pushReplacementNamed('/resetpassword');
          });
        }else{
          Navigator.of(context).pushReplacementNamed('/resetpassword');
        }
      }

    }
  }
}
  initDynamicLinks() async {

    final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
    final Uri deepLink = data?.link;

    if (deepLink != null) {
      _handleDeepLink(deepLink);

    }

    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async {
          final Uri deepLink = dynamicLink?.link;
          _handleDeepLink(deepLink);

        },
        onError: (OnLinkErrorException e) async {
          print('onLinkError');
          print(e.message);
        }
    );
  }
@override
initState(){
initDynamicLinks().then((){
...

});
}


E/flutter(25703):[错误:flutter/lib/ui/ui_dart_state.cc(148)]未处理得异常:NoSuchMethoderRror:调用方法“祖先StateOfType”时为NULL.E/flutter(25703):接收器:空E/flutter(25703):尝试调用:祖先StateOfType(“TypeMatcher”的实例)E/flutter(25703):#0 object.nosuchMethod(dart:core-patch/object_patch.dart:50:5)

共有1个答案

韩华美
2023-03-14

这就是图书馆本身的问题。因为您访问的context在当前是不可访问的。因此,我建议您在MaterialAppCupertinoApp中创建全局导航器状态

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _navigatorKey = GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      key: _navigatorKey,
      home: Container(),
    );
  }
}
 类似资料: