Easy way to Format a Numbers in Flutter

We can easily format any numbers which are coming from API… This is an easiest way to do it.

First you need flutter dependency which is call intl 0.17.0 and you can get it from here : (https://pub.dev/packages/intl). So after you need to adding this to your project as a dependency.

Now its time to code.

As for my experience, This is the way I used to format.

1. you need to import inil.dart

import 'package:intl/intl.dart';

2. Now create local variable for assign value Which is come from API.

var valueConvert; // this is null variable....

2.1 Also you need to create final variable for use numberFormat method

final formatter = NumberFormat("#,###"); 
// here you need to mention pattern how you need to format.
//example : this is the way I want: "#,###" Must note : (pattern type required as String)

3. Now its time to assign snapshot data to our locally created variable

valueConvert = snapshot.data?.data.localNewCases;

//data is my model class
//localNewCases is data class variable which is assign to data json //receiving data from API

4. Finally its time to format using earlier created final variable (numberFormat)

Here I used inside the padding Text widget as a child

formatter is earlier created numberFormat variable and use its format method and pass our value to format.

child: Text(
"${formatter.format(valueConvert)}",// here the way I assigned.
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: const Color(0xffFFFFFF),
),
),

Before format : 1234

After format : 1,234

Mobile App Developer