fluttergoogle-cloud-firestoremailer

How can I include the information from my database in firestore the mail I send through Mailer?


currently I can read the information stored in my database displayed in my application but what I want now is with a button to send that information by email for mailer and I managed to implement that by pressing the "alert" button I send an email but I can not find how to do so that the stored information is sent as I show it.

Here is my code of how I show the information that the user enters the database through a form and this is where I show it.

                          class _readDataState extends State<readData> {
                        final Stream<QuerySnapshot> users = FirebaseFirestore.instance
                            .collection('users')
                            .orderBy('Fecha ingreso sintoma', descending: true)
                            .snapshots();

                        deletedata(id) async {
                          await FirebaseFirestore.instance.collection('users').doc(id).delete();
                        }

                        @override
                        Widget build(BuildContext context) {
                          return Scaffold(
                              appBar: AppBar(
                                title: Text('Sintomas'),
                                backgroundColor: Color.fromARGB(255, 230, 57, 137),
                                elevation: 0.0,
                              ),
                              body: SingleChildScrollView(
                                child: Column(
                                  children: [
                                    Container(
                                      height: 730,
                                      padding: const EdgeInsets.symmetric(vertical: 5),
                                      child: StreamBuilder<QuerySnapshot>(
                                        stream: users,
                                        builder: (
                                          BuildContext context,
                                          AsyncSnapshot<QuerySnapshot> snapshot,
                                        ) {
                                          if (snapshot.hasError) {
                                            return Text('Algo salio mal');
                                          }
                                          if (snapshot.connectionState == ConnectionState.waiting) {
                                            return Text('Cargando');
                                          }
                                          final data = snapshot.requireData;
                                          return ListView.builder(
                                            itemCount: data.size,
                                            itemBuilder: (context, index) {
                                              return Card(
                                                child: Column(
                                                  //mainAxisSize: MainAxisSize.min,
                                                  crossAxisAlignment: CrossAxisAlignment.center,
                                                  mainAxisSize: MainAxisSize.min,
                                                  children: <Widget>[
                                                    Text('Síntomas'),
                                                    Text(
                                                      '${data.docs[index]['Fecha ingreso sintoma']}',
                                                    ),
                                                    Text('''
                                                        Fatiga: ${data.docs[index]['Fatiga'] ? "Si" : "No"}
                                                        Miccion: ${data.docs[index]['Miccion'] ? "Si" : "No"}
                                                        Flujo Vaginal: ${data.docs[index]['Flujo Vaginal'] ? "Si" : "No"}
                                                        Estreñimiento:  ${data.docs[index]['Estreñimiento'] ? "Si" : "No"}
                                                        Acidez Gastrica: ${data.docs[index]['Acidez Gastrica'] ? "Si" : "No"}
                                                        Sangrado Nasal: ${data.docs[index]['Sangrado Nasal'] ? "Si" : "No"}
                                                        Sangrado de encias: ${data.docs[index]['Sangrado de encias'] ? "Si" : "No"}
                                                        Hinchazon: ${data.docs[index]['Hinchazon'] ? "Si" : "No"}
                                                        Problemas respiratarios: ${data.docs[index]['Problemas respiratarios'] ? "Si" : "No"}
                                                        '''),
                                                    Row(
                                                        mainAxisAlignment: MainAxisAlignment.center,
                                                        crossAxisAlignment: CrossAxisAlignment.center,
                                                        children: [
                                                          TextButton(
                                                            child: Text("¡Alertar!"),
                                                            onPressed: sendEmail,
                                                          ),
                                                          GestureDetector(
                                                            onTap: () {
                                                              deletedata(data.docs[index].id);
                                                              setState(() {});
                                                            },
                                                            child: Icon(
                                                              Icons.delete,
                                                              color: Colors.red,
                                                            ),
                                                          ),
                                                        ]),
                                                  ],
                                                ),
                                                elevation: 6,
                                                shadowColor: Color.fromARGB(255, 230, 57, 137),
                                                margin: EdgeInsets.all(15),
                                                shape: OutlineInputBorder(
                                                    borderRadius: BorderRadius.circular(10),
                                                    borderSide: BorderSide(color: Colors.white)),
                                              );
                                            },
                                          );
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ));
                        }

This is where I sent the email and I already tried adding the information to the text but it does not allow me

              Future sendEmail() async {
                  final user = await GoogleAuthApi.signIn();

                  if (user == null) return;

                  final email = user.email;
                  final auth = await user.authentication;
                  final token = auth.accessToken!;

                  print('Authenticated: $email');
                  GoogleAuthApi.signOut();

                  final smtpServer = gmailSaslXoauth2(email, token);
                  final message = Message()
                    ..from = Address(email, 'Aplicacion contro embarazos alto riesgo!')
                    ..recipients = ['gx_13@hotmail.com']
                    ..subject = '¡¡Alerta!! '
                    ..html =
                        '<!DOCTYPE html><html><head><meta charset="utf-8"><title>HTML</title><meta name="viewport" content="width=device-width, initial-scale=1.0"<link rel="stylesheet" href="estilo.css"></head><body><p>Esta página web es una página HTML válida.</p></body></html>';
                  //..text = '';

                  try {
                    await send(message, smtpServer);

                    showSnackBar('sAlerta Enviada');
                  } on MailerException catch (e) {
                    print(e);
                  }
                }

                void showSnackBar(String text) {
                  final snackBar = SnackBar(
                    content: Text(
                      text,
                      style: TextStyle(fontSize: 20),
                    ),
                    backgroundColor: Colors.green,
                  );
                  ScaffoldMessenger.of(context)
                    ..removeCurrentSnackBar()
                    ..showSnackBar(snackBar);
                }
              }

This is how it looks in the app. in the alert button the mail is sent

enter image description here

and this is how the mail arrives

enter image description here


Solution

  • where the alert button is instead of "onPressed: sendEmail" you put "onPressed: () => sendEmail(data)" in the sendEmail function the parameters are added and they can be used to include the database data data in the mail

    onPressed: () => sendEmail(data, index),
    
    Future sendEmail(QuerySnapshot<Object?> data, index) async {