Home > Enterprise >  how to make return of model class into list
how to make return of model class into list

Time:10-10

i have model called LoginPageModel, but the problem is my api have more than one CODD_VALU and CODD_DESC. and i dont know how to return it as list or an array.

This is my LoginPageModel

class LoginPageModel {
  String CODD_VALU;
  String CODD_DESC;

  LoginPageModel({required this.CODD_VALU, required this.CODD_DESC});

  static Future<LoginPageModel> connect(String CODD_VALU) async {
    Uri url = Uri.parse("http://deltaprima.rmdoo.com/api/niv/all");

    var response = await http.get(
      url,
      headers: {
        "CompanyCode": CODD_VALU,
      },
    );

    var dataJson = jsonDecode(response.body);

    return LoginPageModel(
      CODD_VALU: dataJson[0]["CODD_VALU"],
      CODD_DESC: dataJson[0]["CODD_DESC"],
    );
  }
}

and this is response of my api (Im using postma)

[
    {
        "CODD_DESC": "DELTA PRIMA",
        "CODD_VALU": "01"
    },
    {
        "CODD_DESC": "SAMPLE",
        "CODD_VALU": "02"
    }
]

and also this is how i will call return value of LoginPageModel Like this

LoginPageModel.connect(data["CODD_VALU"]).then((value) {
        print(value.CODD_DESC);
        setState(() {
          dataOffice = value.CODD_DESC;
        });
      }

CodePudding user response:

import 'package:wnetworking/wnetworking.dart';

class Offices {
  final String name, value;
  /* ---------------------------------------------------------------------------- */
  Offices(this.name, this.value);
  /* ---------------------------------------------------------------------------- */
  @override
  String toString() => '$name [$value]';
}

class DeltaPrima {
  DeltaPrima._();
  /* ---------------------------------------------------------------------------- */
  static const _url = 'http://deltaprima.rmdoo.com/api/office/all';
  /* ---------------------------------------------------------------------------- */
  static Future<List<Offices>?> getOffices(String token) async {
    var result = (await HttpReqService.get<List>(_url, headers: {'CompanyCode':token}));
    if (result == null) return null;
    var list = result
      .cast<JMap>()
      .map((m) => Offices(m['CODD_DESC']!, m['CODD_VALU']!))
      .toList()
    ;
    print('Objects => $list');
    return list;
  }
}

void main(List<String> args) async {
  await DeltaPrima.getOffices('MW');
  print('Job done!');
}

Output:

Objects => [DELTA PRIMA [01], SAMPLE [02]]
Job done!

CodePudding user response:

The easiest way is as follows:

You need to use a tool that passes Json to Dart class. For example from https://app.quicktype.io and the result is:

// To parse this JSON data, do
//
//     final loginPageModel = loginPageModelFromJson(jsonString);

import 'dart:convert';

List<LoginPageModel> loginPageModelFromJson(String str) =>
    List<LoginPageModel>.from(
        json.decode(str).map((x) => LoginPageModel.fromJson(x)));

String loginPageModelToJson(List<LoginPageModel> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class LoginPageModel {
  LoginPageModel({
    required this.coddDesc,
    required this.coddValu,
  });

  String coddDesc;
  String coddValu;

  factory LoginPageModel.fromJson(Map<String, dynamic> json) => LoginPageModel(
        coddDesc: json["CODD_DESC"],
        coddValu: json["CODD_VALU"],
      );

  Map<String, dynamic> toJson() => {
        "CODD_DESC": coddDesc,
        "CODD_VALU": coddValu,
      };
}

CodePudding user response:

Update Your Login Page Model Like this.

 class LoginPageModel {
      String CODD_VALU;
      String CODD_DESC;
    
      LoginPageModel({required this.CODD_VALU, required this.CODD_DESC});

     factory LoginPageModel.fromJson(Map<String, dynamic> json) => LoginPageModel(
        coddDesc: json["CODD_DESC"],
        coddValu: json["CODD_VALU"],
      );
    
      static Future<List<LoginPageModel>> connect(String CODD_VALU) async {
        Uri url = Uri.parse("http://deltaprima.rmdoo.com/api/niv/all");
    
        var response = await http.get(
          url,
          headers: {
            "CompanyCode": CODD_VALU,
          },
        );
    
        var dataJson = jsonDecode(response.body);
        Iterable l = json.decode(response.body);
        List<LoginPageModel> listmodel = List<LoginPageModel>.from(l.map((model)=> LoginPageModel.fromJson(model)));
    
        return listmodel;
      }
    }
  • Related