Home > OS >  How to convert string type variable to int and do equation on flutter?
How to convert string type variable to int and do equation on flutter?

Time:11-03

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 {}
}


CodePudding user response:

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

Example

int amount = (int.tryParse(oneAmount?.amount) ?? 0)   (int.tryParse(amountController.text) ?? 0);

CodePudding user response:

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));
  • Related