Ashtav
Ashtav

Reputation: 2966

How to set state after dispose() in flutter?

I have 2 pages, in the first page I have a button which is on click will open second page, in second page I have variable number = 999; so when I back to the first page I want to show the number print(number); or display on Text(number) How to do it with dispose() ?

@override
void dispose() {
    super.dispose();
    // send data to the first page
}

thanks for your answer

Upvotes: 4

Views: 2939

Answers (2)

Fernando Lozada
Fernando Lozada

Reputation: 347

With dispose() you need override the back pressed, to do this wrap the Scaffold in WillPopScope widget.

return WillPopScope(
  onWillPop: () {
    _backPressed();
    return Future.value(false);
  },
  child: Scaffold(
    appBar: AppBar(
      title: Text('your text'),
    ),
    body: Center(),
  ),
);

void _backPressed() {
  Navigator.pop(context, '999');
}

Upvotes: 0

anmol.majhail
anmol.majhail

Reputation: 51186

You can simply do this with the help of a navigator.

Navigator.push returns a Future that completes after calling Navigator.pop on the Second Screen with the value passed.

e.x code:

import 'package:flutter/material.dart';

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

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

class SelectionButton extends StatefulWidget {
  @override
  _SelectionButtonState createState() => _SelectionButtonState();
}

class _SelectionButtonState extends State<SelectionButton> {
  String _resultNumber;

  @override
  Widget build(BuildContext context) {
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          RaisedButton(
            onPressed: () => _navigateAndDisplaySelection(context),
            child: Text('Pick an option, any number!'),
          ),
          Text(_resultNumber ?? ''),
        ]);
  }

  _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()),
    );
    _resultNumber = result;
  }
}

class SelectionScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Pick a number'),
      ),
      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, '999');
                },
                child: Text('999 Number'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  // Close the screen and return "Nope!" as the result.
                  Navigator.pop(context, '500');
                },
                child: Text('550 Number'),
              ),
            )
          ],
        ),
      ),
    );
  }
}

enter image description here

Upvotes: 1

Related Questions