Home > Net >  How to get Local JSON in flutter normal class
How to get Local JSON in flutter normal class

Time:11-15

Services-> math_logic.dart it is just maths calculation page so I don't used for StateFul Widget I got error for Futur is not a subtype of String future is not a type Strint As cast...

import 'package:flutter/services.dart';

class Logic {
//Get JSON
  final response =
      rootBundle.loadString('assets/file/Data.json').then((value) => value);
  
//Vehicle Age Method
  vehicleAge(DateTime doPurchase, DateTime doRenewel) {
    var vAge = doPurchase.difference(doRenewel).abs().inDays.toInt();
    return vAge;
  }

 

// below Five Years Method

  belowFiveYear(int age, int fiveyear, int getInt, String zone,
      String odBasicRate, int basicTP) {
    response.then((value) => {print(value)});

    if (age <= fiveyear && getInt == 76150 && zone == 'Zone A') {
      odBasicRate = response[0]['Zone A']; //Error Here, don't get a json Data
      basicTP = 752;
      return [odBasicRate, basicTP];
    }
    
    
  }
}

Given below my json Data Data.json

[
  
  {
    "Product no": 1,
    "Type of Vehicle": "Two Wheeler",
    "Vehicle Age": "Vehicle Age <= 5 years",
    "CC": "Upto 150 CC",
    "Zone A": "1.708",
    "Zone B": "1.676",
    "Zone C": "NA",
    "TP Premium": "-",
    "Per passenger": "-",
    "LT TP Premium": "-",
    "Option 1": "-",
    "Option 2": "-",
    "Discount": "-",
    "Rate percentage": "-"

  }etc..

[My code page is below][1]
  
]

CodePudding user response:

loadString is an asynchronous method, and thus returns a Future<String>. The Future<T>.then() method also returns a Future, so your response variable is actually of type Future<String>

In short you need a method defined as async somewhere to await for the method to complete. This thread gives some insight and examples into initializing an instance member that depends on an async call.

  • Related