0

I fetched the amount from firestore, And in the UI has text field user can add value to that text field that also string I want summation both that "(oneAmount?.amount)! + (amountController.text)"

code

void displayMessage() {
    if (amountController.text != null) {
      int amount = ((oneAmount?.amount)! + (amountController.text)) as int;

      FirebaseFirestore.instance
          .collection("recharge")
          .doc("${loggedInUser.uid}")
          .set({
        "amount": amount,
      });
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => const HomeScreen()),
      );
    } else {}
}


Dasun Dola
  • 541
  • 2
  • 16
  • Does this answer your question? [How do I parse a string into a number with Dart?](https://stackoverflow.com/questions/13167496/how-do-i-parse-a-string-into-a-number-with-dart) – OMi Shah Nov 02 '22 at 16:28
  • parse it to int like this int.parse(amountController.text).toInt() – 7eg Nov 02 '22 at 17:12

2 Answers2

1

you should parse the string into int before doing the addition.

Example

int amount = (int.tryParse(oneAmount?.amount) ?? 0) + (int.tryParse(amountController.text) ?? 0);
Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
1

initialize an int like below:

int a =   int.parse(amountController.text).toInt();

Then use the int a in your addition

int amount = ((oneAmount?.amount)! + (a));
7eg
  • 285
  • 3
  • 11