[英]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.