Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在颤振中从api到模型类设置0或空值_Api_Flutter_Dart_Null_Response - Fatal编程技术网

如何在颤振中从api到模型类设置0或空值

如何在颤振中从api到模型类设置0或空值,api,flutter,dart,null,response,Api,Flutter,Dart,Null,Response,我正在使用混合值调用api。一些值返回0,另一些值返回int。但当我调用该api时,出现以下错误: The getter 'actualTotalFee' was called on null. Receiver: null Tried calling: actualTotalFee 我的api回应是: "summary": { "said_fee": 0, "nos": 11, &qu

我正在使用混合值调用api。一些值返回0,另一些值返回int。但当我调用该api时,出现以下错误:

The getter 'actualTotalFee' was called on null.
Receiver: null
Tried calling: actualTotalFee
我的api回应是:

"summary": {
        "said_fee": 0,
        "nos": 11,
        "currentPayable": 0,
        "eximp-sem": 1,
        "sum_of_tution_fee": 173875,
        "common_scholarship": 0,
        "actual_total_fee": 0,
        "special_scholarship": 10000,
        "per_semester_fee": 0,
        "per_semester_fee_without_scholarship": 0,
        "total_paid": 190000,
        "total_current_due": -200000,
        "Due_Up_to_April": -200000,
        "total_due": -200000
      }
我的api调用方法:

// Getting Accounts Summery
  Future<AccountSummery> fetchAccountSummery(int userId) async {
    
    final url =
        Api.baseUrl + 'student_account_info_summary/$userId?token=$authToken';

    try {
      final response = await get(Uri.encodeFull(url));

      final loadedItem = json.decode(response.body);

      if (response.statusCode == 200) {
        return AccountSummery.fromJson(loadedItem['summary']);
      } else {
        throw Exception('Error in getting account summery');
      }
    } catch (error) {
      throw error;
    }
  }
//获取帐户摘要
未来FetchAccountSummy(int userId)异步{
最终url=
Api.baseUrl+“学生帐户信息摘要/$userId?令牌=$authToken”;
试一试{
最终响应=等待获取(Uri.encodeFull(url));
final loadedItem=json.decode(response.body);
如果(response.statusCode==200){
返回accountsummy.fromJson(loadedItem['summary']);
}否则{
抛出异常(“获取帐户摘要时出错”);
}
}捕获(错误){
投掷误差;
}
}
我的模型调用:

class AccountSummery {
  final int actualTotalFee;
  final int specialScholarship;
  final int perSemesterWithoutScholarship;
  final int perSemesterFee;
  final int totalPaid;
  final double totalCurrentDue;
  final int totalDue;

  AccountSummery(
      {
        this.actualTotalFee,
        this.specialScholarship,
        this.perSemesterWithoutScholarship,
        this.perSemesterFee,
        this.totalPaid,
        this.totalCurrentDue,
        this.totalDue
      });

  factory AccountSummery.fromJson(Map<String, dynamic> json) {
    return AccountSummery(
      actualTotalFee: json['actual_total_fee'],
      specialScholarship: json['special_scholarship'],
      perSemesterWithoutScholarship: json['per_semester_fee_without_scholarship'],
      perSemesterFee: json['per_semester_fee'],
      totalPaid: json['total_paid'],
      totalCurrentDue: json['total_current_due'],
      totalDue: json['total_due'],
    );
  }
}
类会计摘要{
最终实际总费用;
最终国际专业奖学金;
无奖学金的最终奖学金;
最终英特英特费;
最终支付总额;
最终双倍到期;
最终积分到期;
会计摘要(
{
这个,实际的塔尔菲,
这是一项特殊的奖学金,
这是一个没有奖学金的人,
这是珀西梅斯特菲,
这个,这个,,
这是我应得的,
这是我应得的
});
factory AccountSummery.fromJson(映射json){
返回帐户摘要(
ActualToalFee:json['actual_total_fee'],
特殊奖学金:json[“特殊奖学金”],
无奖学金的受迫害者:json[“每学期学费,无奖学金],
perSemesterFee:json[“每学期学费”],
totalPaid:json['total_paid'],
totalCurrentDue:json['total_current_due'],
totalDue:json['total_due'],
);
}
}
由于“实际总费用”、“每学期费用”等响应返回0值,我的api响应方法失败


是否有任何方法可以运行api方法或将0值设置为我的模型类?

如果api具有空值,请使用此方法将默认值设置为0:

actualTotalFee: json['actual_total_fee'] ?? 0,

如果API具有空值,则使用此选项将默认值设置为0:

actualTotalFee: json['actual_total_fee'] ?? 0,
在您的模型中

  actualTotalFee: json['actual_total_fee'] ?? 0,
在你的电话里

AccountSummeryObj?.actualTotalFee ?? 0
在您的模型中

  actualTotalFee: json['actual_total_fee'] ?? 0,
在你的电话里

AccountSummeryObj?.actualTotalFee ?? 0

您可以通过两种方式完成:

  • actualTotalFee:json['actual_total_fee']??0

  • actualTotalFee:json['actual\u total\u fee']=无效的json[“实际费用总额]:0


  • 您可以通过两种方式完成:

  • actualTotalFee:json['actual_total_fee']??0

  • actualTotalFee:json['actual\u total\u fee']=无效的json[“实际费用总额]:0


  • 如果这解决了您的问题,请将问题标记为已回答;)现在新的错误显示:在null上调用了getter“totalCurrentDue”。Receiver:null尝试调用:TotalCurrentDueTottalCurrentDue是双精度的,在我的模型中:totalCurrentDue:json['total\u current\u due']??0.00,在我的呼叫中:double.parse(summy.data.totalCurrentDue?.toStringAsFixed(2)??0),我是否做错了什么?请尝试
    double.parse(summy.data?.totalCurrentDue?.toStringAsFixed(2)?0)
    如果这解决了您的问题,请将问题标记为已回答;)现在新的错误显示:在null上调用了getter“totalCurrentDue”。Receiver:null尝试调用:TotalCurrentDueTottalCurrentDue是双精度的,在我的模型中:totalCurrentDue:json['total\u current\u due']??0.00,在我的调用中:double.parse(summy.data.totalCurrentDue?.toStringAsFixed(2)??0),我是否做错了什么?尝试
    double.parse(summy.data?.totalCurrentDue?.toStringAsFixed(2)?0)