gpt4 book ai didi

flutter - 方法 'fromjson' 没有为类型 'type' 定义

转载 作者:行者123 更新时间:2023-12-05 06:02:30 26 4
gpt4 key购买 nike

方法“fromJson”没有为类型“Type”定义。
尝试将名称更正为现有方法的名称,或定义名为“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 文件并重新创建它。
  • 已经为其他模型实现了相同的代码并进行了改造并且工作正常,只是没有解决这个问题。

最佳答案

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

即:

对于 retrofit 配置:-

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

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

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

关于flutter - 方法 'fromjson' 没有为类型 'type' 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66906825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com