未为 Dart 中的类型定义 getter 'fromJson'

时间:2021-07-30 16:26:28

标签: flutter dart getx

我正在学习 flutter,尝试使用 Getx 的 GetConnect Custom configuration

当尝试为默认请求设置 defaultDecoder 时,它抱怨:

The getter 'fromJson' isn't defined for the type 'Resp'.
Try importing the library that defines 'fromJson', correcting the name to the name of an existing getter, or defining a getter or field named 'fromJson'.dart"

如何修复这个错误?

这是我的代码:

import 'package:get/get.dart';

class AuthService extends GetConnect {
  @override
  void onInit() {
    // All request will pass to jsonEncode so CasesModel.fromJson()

    httpClient.defaultDecoder = Resp.fromJson;
    httpClient.baseUrl = 'http://localhost/app_api/auth';

   
  }

  Future<Response<Map<String, dynamic>>> getCaptcha(String mobile) =>
      get("get_captcha");
}

class Resp {
  final int Errcode;
  final String Errmsg;
  final Map<String, dynamic> Data;

  Resp(this.Errcode, this.Errmsg, this.Data);

  factory Resp.fromJson(Map<String, dynamic> json) {
    return Resp(json["errcode"], json["errmsg"], json["data"]);
  }
}

0 个答案:

没有答案