0

I know that if you had a raiseButton you can do Navigation .... .pop(value);

But what happens if the user goes back and i want to update the value, because result will be null

  Navigator.push(context, MaterialPageRoute(builder: (context) {
                return GalleryClassOne();
              })).then((result) {
                if (result != null) {
                  setState(() {
                    imagesClas1 = result;
                  });
                }
              });
Joan Codinach Ortiz
  • 381
  • 1
  • 2
  • 10
  • Use provider https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple – harsha.kuruwita Nov 20 '20 at 08:34
  • Does this answer your question? [Flutter Back button with return data](https://stackoverflow.com/questions/51927885/flutter-back-button-with-return-data) – Midhun MP Nov 20 '20 at 08:39

2 Answers2

1

You can override the back button behavior with WillPopScope widget. And manually pop with the data you need. Here is the code:

import 'package:flutter/material.dart';

void main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Navigator(
        onGenerateRoute: (settings) => MaterialPageRoute(
          builder: (context) => MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _onButtonPressed() {
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => OtherPage()))
        .then((value) {
      print("returned: $value");
      if (value != null) {
        setState(() {
          // ...
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Demo")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
                child: Text("Open another screen"),
                onPressed: _onButtonPressed),
          ],
        ),
      ),
    );
  }
}

class OtherPage extends StatelessWidget {
  OtherPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        // here you can return anything you need ...
        Navigator.of(context).pop("my value");

        // cancel default behaviour
        return false;
      },
      child: Scaffold(
        appBar: AppBar(title: Text("Other page")),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('Click on return button'),
            ],
          ),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      ),
    );
  }
}
ChessMax
  • 2,125
  • 1
  • 16
  • 16
0

You should return your data at a variable like this

final result = await Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => SelectionScreen(),
  ),
);

The result variable has your data.

for more info, have a look at the docs

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Mohamed Reda
  • 1,207
  • 13
  • 21