How to use double value inside Text widget in flutter?

Viewed 597

I am using the Data table for showing the product lists and monthly revenue. I need to show annual revenue too, but I didn't get annual revenue from JSON. I only get monthly revenue and I need to multiply monthly revenue with 12 to get annual revenue. Here is my code for the data table

DataTable(
            headingRowColor:
            MaterialStateColor.resolveWith((states) => Color(0xFF00105C)),
            columns: [
              DataColumn(
                  label: Text("Product",
                style: TextStyle(
                    fontSize: 12,
                    fontFamily: 'Roboto Bold',
                    color: Colors.white
                ),)),
              // DataColumn(label: Text("Code")),
              DataColumn(label: Text("Revenue(M)",
                style: TextStyle(
                    fontFamily: 'Roboto Bold',
                    color: Colors.white
                ),
              )
              ),
              DataColumn(label: Text("Revenue(A)",
                style: TextStyle(
                    fontSize: 12,
                    fontFamily: 'Roboto Bold',
                    color: Colors.white
                ),)),
            ],
            rows: snapshot.data.map((e) => DataRow(
              cells: [

                DataCell(Text(e.product.productName,
                  style: TextStyle(
                      fontFamily: 'Roboto Regular',
                      color: Colors.black
                  ),),),
                // DataCell(Text(e.product.code)),
                DataCell(Text(e.expectedRevenue.toString(),
                  style: TextStyle(
                      fontFamily: 'Roboto Regular',
                      color: Colors.black
                  ),)),
                DataCell(Text(e.expectedRevenue*12.toString(),
                  style: TextStyle(
                      fontFamily: 'Roboto Regular',
                      color: Colors.black
                  ),)),

              ]
            )).toList(),
          ),

expectedRevenue is a dynamic data type in JSON.

String id;
int leadProductId;
int leadId;
int productId;
dynamic expectedRevenue;
DateTime createdDate;
int createdBy;
int recordStatus;
dynamic lead;
Product product;

Here I got an error for the annual revenue data cell

type 'String' is not a subtype of type 'num' of 'other'

How to solve this issue? How to find the total value of Revenue(M) column data cells?

4 Answers

Should replace your code with ;

(e.expectedRevenue*12).toString()

Try this

double.parse(e.expectedRevenue)*12

you set the "expectedRevenue" as dynamic so possibly it could be a string or any other data type so either set the num/double/int to "expectedRevenue" or parse it with double/int/number.

And then for set it to the Text Widget wrap it with () (Brackets) and set .toString() or toStringAsFixed(2) for setting only two digits after the number

Try This:

double expectedRevenue;

Text((e.expectedRevenue*12).toStringAsFixed(2)),

or

dynamic expectedRevenue;

Text((num.parse(e.expectedRevenue.toString())*12).toStringAsFixed(2))

Understanding the error

expectedRevenue is of type Dynamic. So you will get an error when you multiply it by '12' which is an int


Solution

you need to parse it to int or double as follows -

int.parse(e.expectedRevenue);
double.parse(e.expectedRevenue);

You can now multiply it by 12 to calculate annual revenue. And then use the .toString() method to convert an int or double to a String

Related