2

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?

8
  • 1
    (e.expectedRevenue*12).toString() give a try Commented Jul 2, 2021 at 5:53
  • @Muhtar it's working. Thanks. Can you suggest a solution to find sum of monthly and annual revenue columns? Commented Jul 2, 2021 at 6:00
  • @siyascs is my solution not working? Commented Jul 2, 2021 at 6:03
  • Isnt it annual revenue? Could not get what you mean? Could you clarify? Commented Jul 2, 2021 at 6:04
  • @Muhtar here I have three columns product, monthly revenue, and annual revenue. I list each product and its monthly and annual revenues. suppose I listed five products with their revenues I need to find the total sum of all monthly revenue and annual revenue Commented Jul 2, 2021 at 6:08

4 Answers 4

3

Should replace your code with ;

(e.expectedRevenue*12).toString()
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

double.parse(e.expectedRevenue)*12

Comments

1

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))

Comments

0

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

1 Comment

I have used .toString() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.