颤振导航到启动消息的另一页上,单击

时间:2020-07-20 13:27:57

标签: android flutter dart navigation firebase-cloud-messaging

当用户单击onLaunch或onResume通知时,我试图导航到页面(ViewOrder)。

我的代码看起来像

@override
  void initState() {
    super.initState();
   

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        showMyDialog(message);
      },onResume: (Map<String, dynamic> message) async {
      return _gotoPage(message);
    }, onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        _gotoPage(message);
      }
    );
}

showMyDialog方法是-可以正常工作

void showMyDialog(message) {
  Widget okButton = FlatButton(
    color: Color(0xFFf83a4e),
    child: Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.end,

      children: <Widget>[
        Text("View Details", style: TextStyles.orderId(navigatorKey.currentState.overlay.context).merge(TextStyle(color: Colors.white)),),
      ],
    ),
    onPressed: (){
      if(int.parse(message['data']['not_type'])==1 && int.parse(message['data']['or_id'])>0){
        Navigator.pushReplacement(navigatorKey.currentState.overlay.context,new MaterialPageRoute(builder: (context) => ViewOrder(241)));
      }
      else {
        Navigator.of(navigatorKey.currentState.overlay.context).pop();
      }
    },
  );

  Widget cancelButton = FlatButton(
    color: Color(0xFFdddddd),
    child: Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: <Widget>[
        Text("Cancel", style: TextStyles.orderId(navigatorKey.currentState.overlay.context),),
      ],
    ),
    onPressed: (){
        Navigator.of(navigatorKey.currentState.overlay.context).pop();
    },
  );

showDialog(context: navigatorKey.currentState.overlay.context, child:
    new AlertDialog(
      title: new Text(message['notification']['title'],style: TextStyles.lineStyle(navigatorKey.currentState.overlay.context),),
      content: new Text(message['notification']['body'],style: TextStyles.orderId(navigatorKey.currentState.overlay.context)),
      actions: [
        cancelButton,
        okButton
      ],
    )
);
}

_gotoPage方法是

Future<dynamic> _gotoPage(message){
  print("Message is "+message.toString());
  if(int.parse(message['data']['not_type'])==1 && int.parse(message['data']['or_id'])>0){
    
    Navigator.of(navigatorKey.currentState.overlay.context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) =>
    ViewOrder(int.parse(message['data']['or_id']))), (Route<dynamic> route) => false);
    
  }
 return Future<void>.value();
}

调用_gotoPage方法时,它将转到ViewOrder页面,但显示以下错误

E/flutter ( 5765): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.
E/flutter ( 5765): Receiver: null
E/flutter ( 5765): Tried calling: findAncestorStateOfType<NavigatorState>()
E/flutter ( 5765): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter ( 5765): #1      Navigator.of (package:flutter/src/widgets/navigator.dart:2115:19)
E/flutter ( 5765): #2      Navigator.pushReplacement (package:flutter/src/widgets/navigator.dart:1781:22)

0 个答案:

没有答案