'flutter email sender as a background process

so i'm trying to send an email in flutter as a background process without having to navigate through the gmail app and i followed this tutorial https://www.youtube.com/watch?v=RDwst9icjAY and every thing is working except 'onpressed(){}'function on the iconbutton it showes an erorr of 'Local variable 'sendEmail' can't be referenced before it is declared. Try moving the declaration to before the first use, or renaming the local variable so that it doesn't hide a name from an enclosing scope.' with no quick fixes so what seems to be the problem?, i tried initialising it in multiple places but i guess i'm new to these types of funtions. here is my code

    import 'package:flutter/material.dart';
    import 'package:mailer/mailer.dart';
    import 'package:mailer/smtp_server.dart';
    import 'package:google_sign_in/google_sign_in.dart';

    import 'google_auth_api.dart';

    class emailsend extends StatefulWidget {
    const emailsend({Key? key}) : super(key: key);

    @override
    State<emailsend> createState() => _MainPageState();
    }
    class _MainPageState extends State<emailsend> {

    @override
      Widget build(BuildContext context) {
      return Scaffold(
      appBar: AppBar(
        title: Text('email'),
        centerTitle: true,
      ),
      body: Center(
        child: IconButton(
          icon: Icon(Icons.circle_rounded),
          splashColor: Colors.red,
          color: Colors.red,
          iconSize: 250,
          onPressed:(){ sendEmail},
        ),
      ),
    );

    Future sendEmail() async {
      final user = await GoogleAuthApi.signIn();
      if (user == null) return;
      final email = '[email protected]';
      final auth = await user.authentication;
      final accessToken = '';
      final smptServer = gmailSaslXoauth2(email, accessToken);
      final message = Message()
        ..from = Address(email, 'Khaled')
        ..recipients = ['[email protected]']
        ..subject = 'Hello'
        ..text = 'this is atext email';
      try {
        await send(message, smptServer);
        showSnackBar('sent successfully');
      } on MailerException catch (erorr) {
        print(erorr);
      }
    }
  }
    void showSnackBar(String text) {
    final snackBar = SnackBar(
      content: Text(
        text,
        style: TextStyle(fontSize: 20),
      ),
      backgroundColor: Colors.green,
    );
    ScaffoldMessenger.of(context)
      ..removeCurrentSnackBar()
      ..showSnackBar(snackBar);
  }
}    

// the second page

import 'package:google_sign_in/google_sign_in.dart';
 class GoogleAuthApi {
 static final _googleSignIn =
  GoogleSignIn(scopes: ['https://mail.google.com/']);
 static Future<GoogleSignInAccount?> signIn() async {
  if (await _googleSignIn.isSignedIn()) {
  return _googleSignIn.currentUser;
   } else {
   return await _googleSignIn.signIn();
 }
} 
}


Solution 1:[1]

You are missing an actual call to a function.

onPressed: () { sendEmail(); }

Or if you want to use a reference.

onPressed: sendEmail

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 user18309290