0

I am trying to get data back from the second screen to the first one:

 ...
  onPressed: () {
    sendDataBack(context);
 },
...
  void sendDataBack(BuildContext context) {
    int minSendBack = int.parse(minValueController.text);
    int maxSendBack = int.parse(maxValueController.text);
    Navigator.pop(context,...);
  }

When I use Navigator.pop(context, MaterialPageRout(builder: (context) => main(...))) I get the error the return type 'void' isn't a 'Widget'. How do I pass two variables back?

Sam324
  • 199
  • 4
  • 13
  • 1
    Does this answer your question? [Flutter Back button with return data](https://stackoverflow.com/questions/51927885/flutter-back-button-with-return-data) – Mo Meshkani Sep 30 '20 at 13:03
  • take a look at this: https://flutter.dev/docs/cookbook/navigation/returning-data – Er1 Sep 30 '20 at 13:04
  • But I want to pass both values ​​on one button click. The examples pass one value. I cannot write ```Navigator.pop (context, minValue, maxValue)``` – Sam324 Sep 30 '20 at 13:35
  • If you want to return two or more variables then use a `Map` in `Navigator.pop()` method – pskink Sep 30 '20 at 13:45

2 Answers2

3

Take a look at the following example:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Returning Data',
    home: HomeScreen(),
  ));
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Returning Data Demo'),
      ),
      body: Center(child: SelectionButton()),
    );
  }
}

class SelectionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        _navigateAndDisplaySelection(context);
      },
      child: Text('Pick an option, any option!'),
    );
  }

  // A method that launches the SelectionScreen and awaits the result from
  // Navigator.pop.
  _navigateAndDisplaySelection(BuildContext context) async {
    // Navigator.push returns a Future that completes after calling
    // Navigator.pop on the Selection Screen.
    final result = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => SelectionScreen()),
    );

    // After the Selection Screen returns a result, hide any previous snackbars
    // and show the new result.
    Scaffold.of(context)
      ..removeCurrentSnackBar()
      ..showSnackBar(SnackBar(content: Text("$result")));
  }
}

class SelectionScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pick an option'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  // Close the screen and return "Yep!" as the result.
                  Navigator.pop(context, 'Yep!');
                },
                child: Text('Yep!'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  // Close the screen and return "Nope!" as the result.
                  Navigator.pop(context, 'Nope.');
                },
                child: Text('Nope.'),
              ),
            )
          ],
        ),
      ),
    );
  }
}

And read flutter docs carefully. It's taken from Flutter.dev

Mo Meshkani
  • 1,556
  • 2
  • 18
  • 27
2

you can try this:

in secondScreen Class:

...
  onPeressed () {
    Navigator.pop(context, returnedData);
  }
...

in firstScreen Class:

... 
   onPeressed () {
      result = await Navigator.push(
            context,
            new MaterialPageRoute(
               builder: (context) => new secondScreen();
   }
...
Mohammad Shamsi
  • 521
  • 4
  • 11