Home > Back-end >  How would return a value from a dialog without using a global variable or a state management library
How would return a value from a dialog without using a global variable or a state management library

Time:04-24

How would return a value from a dialog without using a global variable or a state management library?

CodePudding user response:

you can use then

    showDialog(
    context: context,
    builder: (_) {
      return AlertDialog(
        title: Text('Wanna Exit?'),
        actions: [
          FlatButton(
            onPressed: () => Navigator.pop(context, false), // passing false
            child: Text('No'),
          ),
          FlatButton(
            onPressed: () => Navigator.pop(context, true), // passing true
            child: Text('Yes'),
          ),
        ],
      );
    }).then((exit) {
  if (exit == null) return;

  if (exit) {
    // user pressed Yes button
  } else {
    // user pressed No button
  }
});

CodePudding user response:

You can access any data you want by creating an external class which stores the values you want to access.

Let's say for example when a user logs in to my app, I want his/her credentials to be stored so I can access it everywhere throughout the app.

I create a class:

class UserData {
  final String id;
  final String name;
  final String email;
  final String token;

  UserData({
    this.id = '',
    this.name = '',
    this.email = '',
    this.token = '',
  });
}

UserData _currentUser = UserData();

I create a method in that same class file to set the User Data:

void setUserData({
  required String id,
  required String name,
  required String email,
  String token = '',
}) {
  _currentUser = UserData(id: id, name: name, email: email, token: token);
}

Finally I write the getters as well to access this data anywhere from the app:

String get userID {
  return _currentUser.id;
}

String get userName {
  return _currentUser.name;
}

String get userEmail {
  return _currentUser.email;
}

String get userToken {
  return _currentUser.token;
}

Now you call the setUserData method anywhere from your app, you only need to import this class file to that particular code where you're calling this method. Here's an example where if a user logs in to the app, his credentials can be stored like this:

import 'userdata.dart'; // The file where all the above code was written
LoadingButton(
  onClick: () => setUserData('id', 'email', 'name', 'token'),
);

This data will be available anywhere in your app and it is preserved as long as your app is in memory.

To access this data you only need to call one of it's getters anywhere in your app:

import 'userdata.dart';
print(userID);
print(userEmail);
print(userName);
print(userToken);

I used this in one of my projects so I could access the userToken and send authentication requests anywhere from the app.

I have actually combined this with shared_preference to preserve the data even after the app is closed but in your case this feels enough as you only want a value from a dialog box. You can call the setData method in the dialog box widget and call the getters when you need the values back. Hope this helps! Please consider upvoting this answer if it helped you. Feel free to ask anything if you don't understand something.

  • Related