1

我正在通过我的颤振应用程序对我的服务器进行 api 调用,这是我向 API 发出请求的代码:

import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutterbutyle/models/models.dart';
import 'package:flutterbutyle/repository/resources.dart';
import 'package:flutterbutyle/repository/response/response_body_provider.dart';
import 'package:flutterbutyle/repository/service_numbers.dart';
import 'package:http/http.dart' as http;

class ProviderApiClient {
  final http.Client httpClient;

  ProviderApiClient({@required this.httpClient}) : assert(httpClient != null);

  Future<List<Provider>> getAllProviders(
      int startRowIndex, int recordCount) async {


    Map map = {
      'data': {
        'sn': getAllProvidersSN,
        'p27': startRowIndex,
        'p28': recordCount,
      },
    };


    final response = await httpClient.post(postApiUrl, headers: {
        HttpHeaders.authorizationHeader: token,
        HttpHeaders.contentTypeHeader:'application/json'
      }, body:json.encode(map));



        if (response.statusCode == 200) {
          final data = json.decode(response.body) as ResponseBodyProvider;

          if (data.hasError) {
            throw Exception(data.error.message);
          }

          else {
            return data.providers.map((rawProvider) {
              return Provider.fromJson(rawProvider);
            }).toList();
          }
        }

        else {
          throw Exception('error fetching posts');
        }

  }

}

正文响应是空的,我认为这是因为我根据请求发送了正文。我首先在 Api 沙盒网站上测试了我的请求,然后在 Postman 中测试了我的请求。它在那里运作良好。

4

0 回答 0