1

Without Making User Sign-in with Google as the below link suggests

How to access logged in email id from google accounts of a phone using flutter.

Here it shows how to access primary mail id from android.

John Ravi
  • 1,190
  • 1
  • 12
  • 21

2 Answers2

2

You got to let him log in with his account first.

1 - make your user sign in using the Google Sign in package https://pub.dartlang.org/packages/google_sign_in

2 - you can sign the returned user to your backend or a firebase backend simply with this (won't explain the whole process here)

3 - you can access all google api (some access scope can be required, scopes have to be provided on step 1) here is the flutter google api to access all google method https://pub.dartlang.org/packages/googleapis

mcfly
  • 774
  • 1
  • 8
  • 18
2

You can do this in flutter via custom platform-channels .Please be careful and up-front to the user when dealing with account, profile, and contact data.

This requires both the following permissions:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

you can use simple_permissions plugin to request/check the access

Open the file MainActivity.java located in the android java folder in the Project.

add the following code to get the emails from the device in the oncreate method

new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
                new MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {
                        if (call.method.equals("getEmailList")) {
                            final List<String> list = new ArrayList<>();
                            AccountManager manager = AccountManager.get(getApplicationContext());
                            Account[] accounts = manager.getAccountsByType("com.google");
                            List<String> possibleEmails = new ArrayList<String>();

                            for (Account account : accounts) {
                                possibleEmails.add(account.name);
                                System.out.print(account.name);
                            }
                            /*Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
                            Account[] accounts = AccountManager.get(getApplicationContext()).getAccounts();
                            for (Account account : accounts) {
                                if (emailPattern.matcher(account.name).matches()) {
                                    primaryEmail = account.name;
                                }
                            }*/
                            result.success(possibleEmails);
                        }
                    }
                }
        );

and you can call the method from your flutter code via the methodchannel

static const platform = const MethodChannel('samples.flutter.io/email');
var emailist   = await platform.invokeMethod('getEmailList');

full sample project

Shyju M
  • 9,387
  • 4
  • 43
  • 48