skip to Main Content

I Hope anyone can help..
How to format number with thousand separator whil this’s data get form API.
My Code is like this:

Future<List<Data>> fetchData() async {
        var url = Uri.parse('https://bps-3301-asap.my.id/api/sensus-tani-rtup');
       final response = await http.get(url);
      if (response.statusCode == 200) {
          var cokk = jsonDecode(response.body);
          return (cokk['data'] as List).map((data) => Data.fromJson(data)).toList();
      } else {
          throw Exception('Unexpected error occured!');
      }
    }

I wanto display data from that API with separate thousand (with ".").
this the code in data table:

    DataCell(
                      Text(
                       data.rtup.toString(),
                      style: const TextStyle(
                         color: Color.fromARGB(255, 17, 17, 17),
                         fontWeight: FontWeight.normal,
                         fontSize: 12,
                        ),
                      ),
                    ),

I want to display rtup and other data cell get from API as number with thousand sepatator. Try many way but still fail.
very appreciated for any one can help. Txh U

Output right now:
enter image description here

Ouput what i want is all the number display with ("." for thusand) like
11.171
18.197

I use formatter with package intl before (and it’s work), but now i m working with datable while datacell get data from API.

PS: Sorry for my english

2

Answers


  1. Chosen as BEST ANSWER

    Thx man! Finally it's work i've already make a function

    class Format {
      static String convertTo(dynamic number, int decimalDigit) {
        NumberFormat currencyFormatter = NumberFormat.currency(
          locale: 'id',
          symbol: '',
          decimalDigits: decimalDigit,
        );
        return currencyFormatter.format(number);
      }
    }
    

    i m just add double.tryParse

    (Format.convertTo( double.tryParse(data.pangan_rtup), 0)), –

    Thx again.


  2. You can use "intl" package for this.
    Just add intl to your project and import it. Then you can use it like this

      String thousandCommaSeperator(String amount){
        var formatter = NumberFormat('#,###,000');
        if(amount.isNotEmpty) {
          return formatter.format(double.tryParse(amount));
        }
        else{
          return 'N/A';
        }
      }
    

    For this example I have used intl: ^0.17.0.
    And I have created a method for changing number. Just call this method in your view.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search