swagger-codegen 在 flutter 项目中

swagger-codegen in flutter project

我是 flutter 的新手,正在尝试集成到库下以在 flutter 项目中使用 Swagger。 https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/dart/swagger

到目前为止我执行的步骤:

1)

added the Path in Pubspec.yaml
     swagger:
        path: /path/swagger-codegen-master/samples/client/petstore/dart/swagger

2) main.dart 文件:

import 'package:swagger/api.dart';

3) 在 Pubspec.yaml 中添加了 swagger 文件,因此我的项目支持 SDK 2.0.0

 environment:
      sdk: ">=2.0.0-dev.68.0 <3.0.0"

它工作正常问题是: 我可以访问 var api_instance = new PetApi(); 这是在其中实施的招摇 api。 我如何使用我的 swagger api 的 url,它与我的项目完全不同 API。 例如 url http://petstore.swagger.io/v2http://student.swagger.io/v2 并具有完全不同的请求、header 和响应参数? 如何根据我的用途自定义它。

这是我link swagger API 文件所遵循的步骤,
1.Generated 使用 https://editor.swagger.io 的 dart 客户端文件
2.Replaced api_client.dart 文件对 BrowserClient()IOClient() 的使用,并导入了所需的库。此 BrowserClient 在我的项目中导致构建错误,因为它包含 Web 依赖项

  class ApiClient {
      String basePath;
      var client = new BrowserClient();//old one
      var client = new IOClient();//should be changed to this

.
.


3.Then 创建了客户端 class 来访问 API,如下所示,

/// Singleton class for ApiClient
class Client {
  ApiClient apiClient;
  AuthApi _authApi;

  AuthApi get authApi => _authApi;
  static final Client _client = Client._initClient();

  factory Client() {
    return _client;
  }

  Client._initClient() {
    apiClient = ApiClient(
      basePath: "http://yourproject.apilink",
    );
    _authApi = AuthApi(apiClient);
  }
}


4.Then,

class AuthImpl {
  final Client client;

  AuthImpl(this.client) {}

  Future<User> signIn({
    @required String deviceID,
    @required String deviceType,
    @required String phone,
    @required String password,
  }) async {
    AuthLoginResponse authLoginResponse;
    User user;

    authLoginResponse = await client.authApi
        .authPostLogin(deviceID, deviceType, phone, password);
    user = authLoginResponse.payload;
    return user;
  }}