【问题标题】:How to set 0 or null value from api to Model class in Flutter如何在 Flutter 中将 api 中的 0 或 null 值设置为 Model 类
【发布时间】:2026-01-04 20:10:01
【问题描述】:

我正在调用具有混合值的 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;
    }
  }

我的模特来电:

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 值?

【问题讨论】:

    标签: api flutter dart null response


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在你的模型中

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

      在您的通话中

      AccountSummeryObj?.actualTotalFee ?? 0
      

      【讨论】:

      • 如果解决了您的问题,请将问题标记为已回答;)
      • 现在显示新错误:getter 'totalCurrentDue' 在 null 上被调用。接收方:null 尝试调用:totalCurrentDue
      • totalCurrentDue 是双倍的,在我的模型中:totalCurrentDue: json['total_current_due'] ?? 0.00,在我的通话中:double.parse(summery.data.totalCurrentDue?.toStringAsFixed(2) ?? 0),我做错了吗?
      • 试试double.parse(summery.data?.totalCurrentDue?.toStringAsFixed(2) ?? 0)
      【解决方案3】:

      你可以通过两种方式做到这一点:

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

      2. actualTotalFee: json['actual_total_fee'] !=null ? json['actual_total_fee'] : 0

      【讨论】:

        最近更新 更多