Flutter Receive sharing intent and page change

Viewed 1710

I'm working on a mailing application and i'm stuck when i try to share a file to send it by email with the receive_sharing_intent package

I'm able to get the file in the application with it's path and all, but then i need to redirect the user to the mail editor page. When i try to use this to get to the mailEditor page :

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

I get this error :

[ERROR:flutter/shell/common/shell.cc(213)] Dart Error: Unhandled exception: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

This package being developed by the community i couldn't find much help anywhere on the internet.

I failed to use the ReceiveSharingIntent method anywhere else than the main.dart file, so maybe there's a way to use it directly on my mailEditor page that i didn't find?

If more code is needed here is my ReceiveSharingIntent method :

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,
        MaterialPageRoute(
          builder: (context) => MailEditor(
            message: message,
            editorType: 'new',
          ),
        ),
      ); 
    });

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

Thanks a lot for any help and suggestions, i've been stuck on this for a while.

Edit: Added more code for context

1 Answers

Navigator should be used inside the build() method. If you want to use context outside of it, pass it as an argument like BuildContext context.

Related