首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在颤振中将0或空值从api设置为模型类

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

Stack Overflow用户
提问于 2021-04-24 20:02:00
回答 3查看 2.7K关注 0票数 1

我正在调用一个混合值的api。一些值返回0,另一些值返回int。但是当我调用那个api时,我得到了这个错误:

代码语言:javascript
运行
复制
The getter 'actualTotalFee' was called on null.
Receiver: null
Tried calling: actualTotalFee

我的api响应是:

代码语言:javascript
运行
复制
"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调用方法:

代码语言:javascript
运行
复制
// 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;
    }
  }

我的模特电话:

代码语言:javascript
运行
复制
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'],
    );
  }
}

由于"actual_total_fee“、"per_semester_fee”等响应返回0值,所以我的api响应方法失败了。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-04-24 20:09:34

在你的模型里

代码语言:javascript
运行
复制
  actualTotalFee: json['actual_total_fee'] ?? 0,

在你的电话里

代码语言:javascript
运行
复制
AccountSummeryObj?.actualTotalFee ?? 0
票数 4
EN

Stack Overflow用户

发布于 2021-04-24 20:45:40

你可以用两种方式来做:

  1. actualTotalFee: json['actual_total_fee'] ?? 0

  1. actualTotalFee: json['actual_total_fee'] !=null ? json['actual_total_fee'] : 0
票数 1
EN

Stack Overflow用户

发布于 2021-04-24 20:09:09

如果API有空值,则使用它将默认值设置为0:

代码语言:javascript
运行
复制
actualTotalFee: json['actual_total_fee'] ?? 0,
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67246959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档