skip to Main Content

I have 2 class models, the first class is for displaying a list of banks while the second is for displaying a description of the bank list. The two classes will be called with 2 different URLs using the POST method. The section displaying the bank list has no problems, but to display the bank list description requires an additional path from the first class ID, for example the expected path ‘…./topup/8/desc’ if the number 8 is replaced with another number it will display a description of list of different banks.How to write the URL path correctly.

The following is an example program:
First Class

class ListDataModel{
  final int IdTopup;
  final String NameTopup;
  final String InformTopup;
  final int BinVaTopup;
  final String imgLogoTopup;

ListDataModel ({
    required this.IdTopup,
    required this.NameTopup,
    required this.InformTopup,
    required this.BinVaTopup,
    required this.imgLogoTopup,

factory ListDataModel.fromJson(Map<String, dynamic> json) {
    return ListDataModel(
      IdTopup: json['IdTopup'] as int,
      NamaTopup: json['NameTopup'] as String,
      KeteranganTopup: json['InformTopup'] as String,
      BinVaTopup: json['BinVaTopup'] as int,
      GambarWebTopup: json['imgLogoTopup'] as String,

Second Class

class DescDataModel{
  final int Id;
  final String Name;
  final String Information;
  final int BinVA;
  final String imgLogo;
  final String Description;
  final int NoSerial;

  DescDataModel({
    required this.Id,
    required this.Name,
    required this.Information,
    required this.BinVA,
    required this.imgLogo,
    required this.Description,
    required this.NoSerial
  });

  factory DescDataModel.fromJson(Map<String, dynamic> json){
    return DescDataModel(
        Id: json['Id'] as int,
        Nama: json['Name'] as String,
        Keterangan: json['Information'] as String,
        BinVA: json['BinVa'] as int,
        GambarWeb: json['imgLogo'] as String,
        Deskripsi: json['Description'] as String,
        NoUrut: json['NoSerial'] as int
    );
  }
}

this is API programs

  Future <List<ListDataModel>> ListData() async {
    var responseList = await http.post(Uri.parse(Url + '/topup'),
      headers: <String, String>{
        'x-api-key': 'CmJsAItd7oKoGqZ1',
      },
      body: { },
    );
    if (responseList.statusCode == 200) {
      List jsonResponse = json.decode(responseList.body);
      return jsonResponse.map((dataList) => ListDataModel.fromJson(dataList)).toList();
    } else {
      throw Exception('Loading...');
    }
  }


  Future <List<DescDataModel>> descData() async{
 final responseDesc = await http.post(Uri.parse(Url + '/topup' + '/topup/:IdTopup' +'/desc'), //how can i write the correct url path here
        headers: {
          'x-api-key': 'CmJsAItd7oKoGqZ1',
        },
        body: { }
    );
    if (responseDesc.statusCode == 200) {
      List jsonResponDesc = json.decode(responseDesc.body);
      return jsonResponDesc.map((dataDesc) => DescDataModel.fromJson(dataDesc)).toList();
    } else {
      throw Exception('Loading...');
    }

  }

this is the error :
Exception: Loading...

2

Answers


  1. try this

    final responseDesc = await http.post(
      Uri.parse('$Url/topup/topup/$IdTopup/desc'), // Using string interpolation to build the URL
      headers: {
        'x-api-key': 'CmJsAItd7oKoGqZ1',
      },
      body: {},
    );
    
    Login or Signup to reply.
  2. URL path you’re using in the descData function is incorrect. You’re trying to use a variable IdTopup in the URL path, but it’s not defined in that scope.

    You need to pass the IdTopup as a parameter to the descData function.

    Future <List<DescDataModel>> descData(int idTopup) async{
      final responseDesc = await http.post(Uri.parse(Url + '/topup/$idTopup/desc'), 
        headers: {
          'x-api-key': 'CmJsAItd7oKoGqZ1',
        },
       }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search