Home > Blockchain >  perform calculations on API data in Flutter
perform calculations on API data in Flutter

Time:12-12

permission to ask, I'm a little confused about the problem I'm having, I want to do addition, division, times and other calculations using the API data below, but I'm confused about how I do data calculations in the API, is there anyone here who wants to give an example of calculation of API data? because if using local data in variables I can use but what if the data comes from API. Thank You.

enter image description here

this is an example when i do a local computation on a variable enter image description here

and what if i do calculations if using data from API

Future<NilaiMahasiswa> getNilaiMahasiswa(int semester) async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    final response = await http.get(
      Uri.parse(
        '$url/auth/mhs_siakad/transkrip_nilai?semester=$semester',
      ),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );

    print(response.statusCode);
    print(response.body);
    if (response.statusCode == 200) {
      return NilaiMahasiswa.fromJson(jsonDecode(response.body));
    } else {
      throw Exception();
    }
  }

CodePudding user response:

The problem in your example is that those digits you seeing are actually of String type, not a num type (int or double), so taking this example:

String stringExample = "100";
String stringExample2 = "200";
print(stringExample    stringExample2); // 100200 

because they are both String, it will just concatenate them.

int intExample = 100;
int intExample2 = 200;
print(intExample2   intExample); // 300

will calculate the sum of them because they are actual numbers.

now take an example like your code:

final mapExample = {
"nilay_akhir_uas": "90",
"nilay_akhir": "90.33",
}

we can get the value in this Map with its key like this:

print(mapExample["nilay_akhir_uas"]); // "90"

and trying to do a math calculation over them:

  print(mapExample["nilay_akhir_uas"]!   mapExample["nilay_akhir"]!); // 9090.33

Because as we said the String elements get concatenated even if you see that they contain numbers, it is still a String


So how can we achieve a math calculation over String which contains a number like this?

we need to parse them, and Dart offers a useful method for this, such as int.parse(), int.tryParse()..., so we can do this:

int nilay_akhir_uas = int.parse(mapExample["nilay_akhir_uas"]!); // 90 as int
double nilay_akhir = double.parse(mapExample["nilay_akhir"]!); // 93.33 as double

print(nilay_akhir_uas   nilay_akhir); // 180.33 

The int.parse() and double.parse() will parse a String to a number of its possible, otherwise, it will throw an exception.

if you want to avoid the exception and get a null when it can't parse the String, then consider using int.tryParse() and double.tryParse():

int :

int.parse("10"); // return 10
int.tryParse("10"); // return 10
int.parse("test"); // throws exception
int.tryParse("test"); // return null

double :

double.parse("99.99"); // return 99.99
double.tryParse("99.99"); // return 99.99
double.parse("test"); // throws exception
double.tryParse("test"); // return null
  • Related