Is it possible to convert print data into a string in order for Firestore integration?

Issue

I would like to know whether or not it is possible to utilise the print(); command in flutter to create a String in order to link said data to a Firestore database.

I am creating a customer satisfaction application in which the customer would press a button on the screen and said button would then omit a message to the database corresponding the reaction the customer selected.

Only issue is: I have not found a way to link onPressed:in way that it can omit such data to a server or locally.

Here is a brief bit of code to somewhat witness what I am trying to achieve:

Align(alignment: Alignment.center,
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                onPrimary: Colors.cyanAccent,
                primary: Colors.deepPurple,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(55.0),
                ),
              ),
              onPressed: () async {
                print('Good Chosen');
                print(DateTime.now());
                HapticFeedback.mediumImpact();
                // Close the screen and return "Good Chosen" as the result.
                Navigator.pop(context, 'Good Chosen');
              },
              child: Image.asset('assets/good pixel.png'),
            ),
          ),

Solution

Have you considered writing the Good Chosen string directly to firestore?

Instead of / additionally to your print statements, you could write

FirebaseFirestore.instance.collection("feedback").doc()
     .set({'type': 'good_chosen', 'timestamp': DateTime.now(),}, SetOptions(merge: true));

Answered By – Tiago Santos

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *