【问题标题】:the method 'fromjson' isn't defined for the type 'type'没有为类型“type”定义方法“fromjson”
【发布时间】:2021-06-28 14:06:55
【问题描述】:

没有为类型“Type”定义方法“fromJson”。
尝试将名称更正为现有方法的名称,或定义名为“fromJson”的方法

以下代码在 Retrofit.g.dart 文件中

@override
  Future<Map<String, dynamic>> signupCustomerRegistration(customerReg) async {
    ArgumentError.checkNotNull(customerReg, 'customerReg');
    const _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _data = <String, dynamic>{};
    _data.addAll(customerReg?.toJson() ?? <String, dynamic>{});
    final _result = await _dio.request<Map<String, dynamic>>(
        '/api/API/CustomerSignUp',
        queryParameters: queryParameters,
        options: RequestOptions(
            method: 'POST',
            headers: <String, dynamic>{},
            extra: _extra,
            baseUrl: baseUrl),
        data: _data);
    var value = _result.data.map((k, dynamic v) =>
        MapEntry(k, dynamic.fromJson(v as Map<String, dynamic>)));

    return value;
  }

我的模型文件代码如下:

// To parse this JSON data, do
//
//     final customerReg = customerRegFromJson(jsonString);

import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

part 'CustomerRegModel.g.dart';


@JsonSerializable()
class CustomerRegModel {
  CustomerRegModel({
    this.custUid,
    this.appname,
    this.blacklist,
    this.custEmail,
    this.custName,
    this.custPhone,
    this.fcmToken,
    this.password,
    this.agentnin,
    this.source,
    this.signupDate,
    this.commStartTime,
    this.commEndTime,
    this.commMaxValue,
    this.commMinValue,
    this.commDownValue,
    this.walletamount,
  });

  String custUid;
  String appname;
  bool blacklist;
  String custEmail;
  String custName;
  String custPhone;
  String fcmToken;
  String password;
  String agentnin;
  String source;
  int signupDate;
  int commStartTime;
  int commEndTime;
  int commMaxValue;
  int commMinValue;
  int commDownValue;
  int walletamount;

  factory CustomerRegModel.fromJson(Map<String, dynamic> json) => _$CustomerRegModelFromJson(json);
  Map<String, dynamic> toJson() => _$CustomerRegModelToJson(this);
  
  CustomerRegModel customerRegModelFromJson(String str) => CustomerRegModel.fromJson(json.decode(str));
  String customerRegModelToJson(CustomerRegModel data) => json.encode(data.toJson());
}

试过了:

  • 使缓存无效并重新启动。
  • 删除 .g.dart 文件并重新创建它。
  • 已为其他经过改造的模型实施了相同的代码,并且工作正常,只是没有解决这个问题。

【问题讨论】:

  • 什么是dynamic.fromJson?你不能像这样使用dynamic
  • 没错,将 dynamic.fromJson 替换为 CustomerRegModel.fromJson
  • 它正在使用 Retrofit.g.dart 的 build runner 命令创建自己

标签: flutter dart retrofit pojo


【解决方案1】:

好的,所以在研究并在 Github Retrofit (https://github.com/trevorwang/retrofit.dart/issues/327) 中创建了一个他们没有回应的问题之后,我去搜索将 Json 解析为 Map 并找到了解决问题的方法。

即:

对于改造配置:-

  @GET("/api/API/CustomerLogin")
  Future<String> loginCustomer(@Query('id') String email_or_number,
  @Query('pass') String pass, @Query('check') String check);

请注意,上面我使用字符串作为响应。现在我将使用 json.decode 方法将 String 转换为 Map。

final response = await client.loginCustomer(email_or_number,pass,check);
final Map<String,dynamic> map = json.decode(response);

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2021-09-02
    • 2022-11-21
    • 1970-01-01
    • 2021-10-19
    • 2020-12-14
    • 2021-07-19
    • 2021-09-01
    • 2021-09-17
    相关资源
    最近更新 更多