You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向Map添加键值对时int/double类型不匹配错误及朋友呼叫按钮数据未加载问题求助(附NvOrdersResponseData代码)

Hey there, let's break down and solve your two technical issues step by step:

1. Type Error When Adding Key-Value Pairs to Map

Issue Details

You're hitting the error: Expected a value of type 'int', but got one of type 'double' during JSON parsing/Map operations.

Root Cause

This happens because your backend returns floating-point numbers (like 1.0) for fields that your NvOrdersResponseData class defines as int types (e.g., currentPage, lastPage, from). The json_serializable package can't automatically convert a double to an int without explicit handling.

Fix Options

Option 1: Add Custom JSON Converters

Define a helper function to convert dynamic values to int, then attach it to each affected field using @JsonKey:

import 'package:json_annotation/json_annotation.dart';
import 'package:novadan_admin/global/models/order/nv_order_item.dart';
import 'package:novadan_admin/global/models/order/nv_order_sale_result.dart';
import 'package:novadan_admin/global/models/paginate/links.dart';

part 'nv_orders_response_data.g.dart';

// Helper converter: handles both int and double inputs
int? _dynamicToInt(dynamic value) {
  if (value is double) {
    return value.toInt();
  } else if (value is int) {
    return value;
  }
  return null;
}

@JsonSerializable()
class NvOrdersResponseData {
  @JsonKey(name: 'current_page', fromJson: _dynamicToInt)
  int? currentPage;
  @JsonKey(name: 'data')
  List<NvOrderSaleResult>? data;
  @JsonKey(name: 'first_page_url')
  String? firstPageUrl;
  @JsonKey(name: 'from', fromJson: _dynamicToInt)
  int? from;
  @JsonKey(name: 'last_page', fromJson: _dynamicToInt)
  int? lastPage;
  @JsonKey(name: 'last_page_url')
  String? lastPageUrl;
  @JsonKey(name: 'links')
  List<Links>? links;
  @JsonKey(name: 'next_page_url')
  dynamic nextPageUrl;
  @JsonKey(name: 'path')
  String? path;
  @JsonKey(name: 'per_page', fromJson: _dynamicToInt)
  int? perPage;
  @JsonKey(name: 'prev_page_url')
  dynamic prevPageUrl;
  @JsonKey(name: 'to', fromJson: _dynamicToInt)
  int? to;
  @JsonKey(name: 'total', fromJson: _dynamicToInt)
  int? total;

  NvOrdersResponseData({
    this.currentPage,
    this.data,
    this.firstPageUrl,
    this.from,
    this.lastPage,
    this.lastPageUrl,
    this.links,
    this.nextPageUrl,
    this.path,
    this.perPage,
    this.prevPageUrl,
    this.to,
    this.total,
  });

  factory NvOrdersResponseData.fromJson(Map<String, dynamic> json) {
    return _$NvOrdersResponseDataFromJson(json);
  }

  Map<String, dynamic> toJson() => _$NvOrdersResponseDataToJson(this);
}

// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'nv_orders_response_data.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

NvOrdersResponseData _$NvOrdersResponseDataFromJson(Map<String, dynamic> json) {
  return NvOrdersResponseData(
    currentPage: _dynamicToInt(json['current_page']),
    data: (json['data'] as List<dynamic>?)
        ?.map((e) => NvOrderSaleResult.fromJson(e as Map<String, dynamic>))
        .toList(),
    firstPageUrl: json['first_page_url'] as String?,
    from: _dynamicToInt(json['from']),
    lastPage: _dynamicToInt(json['last_page']),
    lastPageUrl: json['last_page_url'] as String?,
    links: (json['links'] as List<dynamic>?)
        ?.map((e) => Links.fromJson(e as Map<String, dynamic>))
        .toList(),
    nextPageUrl: json['next_page_url'],
    path: json['path'] as String?,
    perPage: _dynamicToInt(json['per_page']),
    prevPageUrl: json['prev_page_url'],
    to: _dynamicToInt(json['to']),
    total: _dynamicToInt(json['total']),
  );
}

Map<String, dynamic> _$NvOrdersResponseDataToJson(
        NvOrdersResponseData instance) =>
    <String, dynamic>{
      'current_page': instance.currentPage,
      'data': instance.data,
      'first_page_url': instance.firstPageUrl,
      'from': instance.from,
      'last_page': instance.lastPage,
      'last_page_url': instance.lastPageUrl,
      'links': instance.links,
      'next_page_url': instance.nextPageUrl,
      'path': instance.path,
      'per_page': instance.perPage,
      'prev_page_url': instance.prevPageUrl,
      'to': instance.to,
      'total': instance.total,
    };

Option 2: Use num Type Instead of int

If you prefer a simpler fix, change the field types to num? (which accepts both int and double), then cast to int when using the value:

@JsonKey(name: 'current_page')
num? currentPage;

// When accessing the value:
int? pageNumber = currentPage?.toInt();

2. Friends Call Button Data Fails to Load

Issue Details

You can't retrieve or load data for the "friends call button" associated with your orders.

Key Checks & Fixes

Since the button data is likely nested inside the data list (each NvOrderSaleResult entry), here's what to investigate:

  1. Verify Backend Response:

    Use tools like Postman or curl to inspect your API's raw JSON response. Confirm that each order in the data array includes the call button data under the correct field name (e.g., friends_call_button, call_button).

  2. Update NvOrderSaleResult Class:
    Ensure your NvOrderSaleResult class includes a field for the call button data, with a @JsonKey that matches the backend's field name exactly. Example:

    @JsonSerializable()
    class NvOrderSaleResult {
      // ... existing fields ...
      @JsonKey(name: 'friends_call_button') // Match backend field name
      NvFriendsCallButton? friendsCallButton;
    
      NvOrderSaleResult({/* include new field in constructor */});
    
      factory NvOrderSaleResult.fromJson(Map<String, dynamic> json) =>
          _$NvOrderSaleResultFromJson(json);
    }
    
    // Define the button data model
    @JsonSerializable()
    class NvFriendsCallButton {
      String? buttonText;
      String? callUrl;
      bool? isEnabled;
    
      NvFriendsCallButton({this.buttonText, this.callUrl, this.isEnabled});
    
      factory NvFriendsCallButton.fromJson(Map<String, dynamic> json) =>
          _$NvFriendsCallButtonFromJson(json);
    }
    
  3. Check UI Layer Logic:
    Make sure you're correctly accessing the button data after parsing the response:

    // After fetching NvOrdersResponseData
    for (var order in responseData.data ?? []) {
      var callButton = order.friendsCallButton;
      if (callButton != null) {
        // Use callButton.buttonText, callButton.callUrl, etc.
      }
    }
    
  4. Regenerate Serialization Code:
    Run flutter pub run build_runner build after adding new fields to update the generated .g.dart files.


内容的提问来源于stack exchange,提问作者emirşah erden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:07:46