[英]How to get Local JSON in flutter normal class
服務-> math_logic.dart 它只是數學計算頁面,所以我不用於 StateFul 小部件我得到的錯誤是 Futur 不是 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];
}
}
}
下面給出我的 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]
]
loadString
是一個異步方法,因此返回一個Future<String>
。 Future<T>.then()
方法也返回一個 Future,所以你的response
變量實際上是Future<String>
類型
簡而言之,您需要一個在某處定義為async
的方法來await
該方法完成。 此線程提供了一些有關初始化依賴於異步調用的實例成員的見解和示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.