'GetX - How to return a value from Get.Dialog?

How can I get a return value from a Get.Dialog that opens a Dialog Widget / AlertDialog?



Solution 1:[1]

1. GetMaterialApp

In your main.dart, ensure MyApp is returning a GetMaterialApp instead of MaterialApp

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp( // <-- Use GetMaterialApp
      title: 'Flutter Demo',
      home: MyHomePage(title: 'Flutter Examples'),
    );
  }
}

This allows Get to handle navigation / routing, making Navigation methods available such as: Get.to(), Get.dialog(), Get.back(), etc. Without GetMaterialApp as the root of your app you'll see a (confusing) error when calling any of the navigation methods:

E/flutter (11139): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 
'package:flutter/src/widgets/localizations.dart': Failed assertion: 
line 453 pos 12: 'context != null': is not true.

2. Get.dialog + Get.back(result: X)

Have your call to Get.dialog expect an async return value ...

onPressed: () async {
  // assign return value to an observable
  return lx.result.value = await Get.dialog(

... which gets returned when the dialog is closed using Get.back(result: X) where X is a dynamic value returned via Get.dialog:

onPressed: () => Get.back(result: true),

Complete example:

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

class LoginX extends GetxController {
  RxBool result = false.obs;
}

class GetDialogReturnPage extends StatelessWidget {
  final LoginX lx = Get.put(LoginX());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GetDialog Return Example'),
      ),
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Container(
              child: Obx(() => Text('Value shows here: ${lx.result.value}')),
            ),
            Container(
              alignment: Alignment.center,
              child: RaisedButton(
                child: Text('Login'),
                onPressed: () async {
                  // ** assign return value to an observable **
                  return lx.result.value = await Get.dialog(
                      AlertDialog(
                          content: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              RaisedButton(
                                child: Text('Good Login'),
                                onPressed: () => Get.back(result: true),
                                // ** result: returns this value up the call stack **
                              ),
                              SizedBox(width: 5,),
                              RaisedButton(
                                child: Text('Bad Login'),
                                onPressed: () => Get.back(result: false),
                              ),
                            ],
                          )
                      )
                  );},
              ),
            )
          ],
        ),
      ),
    );
  }
}

Solution 2:[2]

If you want to return multiple arguments then use result method like below

Get.back(result: [
    {"backValue": "value"}
]);

And as mentioned on above answer you need to insure that you are using GetMaterialApp.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Baker
Solution 2 Paresh Mangukiya