•  


Return data from a screen | Flutter

Return data from a screen

In some cases, you might want to return data from a new screen. For example, say you push a new screen that presents two options to a user. When the user taps an option, you want to inform the first screen of the user's selection so that it can act on that information.

You can do this with the Navigator.pop() method using the following steps:

  1. Define the home screen
  2. Add a button that launches the selection screen
  3. Show the selection screen with two buttons
  4. When a button is tapped, close the selection screen
  5. Show a snackbar on the home screen with the selection

1. Define the home screen

#

The home screen displays a button. When tapped, it launches the selection screen.

dart
class
 HomeScreen
 extends
 StatelessWidget
 {

  const
 HomeScreen
({
super
.key});


  @override

  Widget
 build
(
BuildContext
 context) {

    return
 Scaffold
(

      appBar: 
AppBar
(

        title: 
const
 Text
(
'Returning Data Demo'
),

      ),

      // Create the SelectionButton widget in the next step.

      body: 
const
 Center
(

        child: 
SelectionButton
(),

      ),

    );

  }

}

2. Add a button that launches the selection screen

#

Now, create the SelectionButton, which does the following:

  • Launches the SelectionScreen when it's tapped.
  • Waits for the SelectionScreen to return a result.
dart
class
 SelectionButton
 extends
 StatefulWidget
 {

  const
 SelectionButton
({
super
.key});


  @override

  State
<
SelectionButton
> 
createState
() => 
_SelectionButtonState
();

}


class
 _SelectionButtonState
 extends
 State
<
SelectionButton
> {

  @override

  Widget
 build
(
BuildContext
 context) {

    return
 ElevatedButton
(

      onPressed: () {

        _navigateAndDisplaySelection
(context);

      },

      child: 
const
 Text
(
'Pick an option, any option!'
),

    );

  }


  Future
<
void
> 
_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,

      // Create the SelectionScreen in the next step.

      MaterialPageRoute
(builder: (context) => 
const
 SelectionScreen
()),

    );

  }

}

3. Show the selection screen with two buttons

#

Now, build a selection screen that contains two buttons. When a user taps a button, that app closes the selection screen and lets the home screen know which button was tapped.

This step defines the UI. The next step adds code to return data.

dart
class
 SelectionScreen
 extends
 StatelessWidget
 {

  const
 SelectionScreen
({
super
.key});


  @override

  Widget
 build
(
BuildContext
 context) {

    return
 Scaffold
(

      appBar: 
AppBar
(

        title: 
const
 Text
(
'Pick an option'
),

      ),

      body: 
Center
(

        child: 
Column
(

          mainAxisAlignment: 
MainAxisAlignment
.center,

          children: [

            Padding
(

              padding: 
const
 EdgeInsets
.
all
(
8
),

              child: 
ElevatedButton
(

                onPressed: () {

                  // Pop here with "Yep"...

                },

                child: 
const
 Text
(
'Yep!'
),

              ),

            ),

            Padding
(

              padding: 
const
 EdgeInsets
.
all
(
8
),

              child: 
ElevatedButton
(

                onPressed: () {

                  // Pop here with "Nope"...

                },

                child: 
const
 Text
(
'Nope.'
),

              ),

            )

          ],

        ),

      ),

    );

  }

}

4. When a button is tapped, close the selection screen

#

Now, update the onPressed() callback for both of the buttons. To return data to the first screen, use the Navigator.pop() method, which accepts an optional second argument called result . Any result is returned to the Future in the SelectionButton.

Yep button

#
dart
ElevatedButton
(

  onPressed: () {

    // Close the screen and return "Yep!" as the result.

    Navigator
.
pop
(context, 
'Yep!'
);

  },

  child: 
const
 Text
(
'Yep!'
),

)

Nope button

#
dart
ElevatedButton
(

  onPressed: () {

    // Close the screen and return "Nope." as the result.

    Navigator
.
pop
(context, 
'Nope.'
);

  },

  child: 
const
 Text
(
'Nope.'
),

)

5. Show a snackbar on the home screen with the selection

#

Now that you're launching a selection screen and awaiting the result, you'll want to do something with the information that's returned.

In this case, show a snackbar displaying the result by using the _navigateAndDisplaySelection() method in SelectionButton :

dart
// A method that launches the SelectionScreen and awaits the result from

// Navigator.pop.

Future
<
void
> 
_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) => 
const
 SelectionScreen
()),

  );


  // When a BuildContext is used from a StatefulWidget, the mounted property

  // must be checked after an asynchronous gap.

  if
 (!context.mounted) 
return
;


  // After the Selection Screen returns a result, hide any previous snackbars

  // and show the new result.

  ScaffoldMessenger
.
of
(context)

    ..
removeCurrentSnackBar
()

    ..
showSnackBar
(
SnackBar
(content: 
Text
(
'
$
result
'
)));

}

Interactive example

#
import 'package:flutter/material.dart';

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

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

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

class SelectionButton extends StatefulWidget {
  const SelectionButton({super.key});

  @override
  State<SelectionButton> createState() => _SelectionButtonState();
}

class _SelectionButtonState extends State<SelectionButton> {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        _navigateAndDisplaySelection(context);
      },
      child: const Text('Pick an option, any option!'),
    );
  }

  // A method that launches the SelectionScreen and awaits the result from
  // Navigator.pop.
  Future<void> _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) => const SelectionScreen()),
    );

    // When a BuildContext is used from a StatefulWidget, the mounted property
    // must be checked after an asynchronous gap.
    if (!context.mounted) return;

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

class SelectionScreen extends StatelessWidget {
  const SelectionScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pick an option'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8),
              child: ElevatedButton(
                onPressed: () {
                  // Close the screen and return "Yep!" as the result.
                  Navigator.pop(context, 'Yep!');
                },
                child: const Text('Yep!'),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8),
              child: ElevatedButton(
                onPressed: () {
                  // Close the screen and return "Nope." as the result.
                  Navigator.pop(context, 'Nope.');
                },
                child: const Text('Nope.'),
              ),
            )
          ],
        ),
      ),
    );
  }
}
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본