Flutter接收共享意图和页面更改

问题描述

我正在开发邮件应用程序,当我尝试共享文件以通过receive_sharing_intent package通过电子邮件发送文件时被卡住了

我能够在应用程序中获取文件及其路径和所有内容,但是随后我需要将用户重定向到邮件编辑器页面。当我尝试使用它进入mailEditor页面时:

Navigator.push(
        context,MaterialPageRoute(
          builder: (context) => MailEditor(
            message: message,editorType: 'new',),);

我收到此错误:

[错误:flutter / shell / common / shell.cc(213)] Dart错误:未处理的异常: 请求的导航器操作使用了不包含导航器的上下文。 用于从导航器推送或弹出路由的上下文必须是作为导航器小部件的后代的小部件的上下文。

此软件包是由社区开发的,我在Internet上找不到任何帮助。

除了main.dart文件之外,其他地方我都没有使用ReceiveSharingIntent方法,所以也许有一种方法可以直接在我未找到的mailEditor页面上使用它?

如果需要更多代码,这是我的ReceiveSharingIntent方法:

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MssProState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void dispose() {
    _intentDataStreamSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // For sharing images coming from outside the app while the app is in the memory
    _intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> value) {
      _sharedFiles = value;

      Message message = Message();
      for (var i = 0; i < _sharedFiles.length; i++) {
        File file = File(_sharedFiles[i].path);

        Attachment attachment = Attachment(
          fileName: basename(file.path),attachmentPart: i + 1,contentType: lookupMimeType(file.path),name: basename(file.path),);
      }

        Navigator.push(
        context,); 
    });

    return MaterialApp(
      debugShowCheckedModeBanner: false,home: Login(),routes: {
        '/login': (BuildContext context) => Login(),},);
  }

非常感谢您的帮助和建议,我已经坚持了一段时间。

编辑: 添加了更多上下文代码

解决方法

导航器应在build()方法内使用。如果要在其外部使用context,请将其作为参数传递,例如BuildContext context

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...