alperefesahin
alperefesahin

Reputation: 781

Flutter api request response body gives always instance of something

I have an API, when I do post requests, like this:

 await http.post(url, body: jsonEncode(body), headers: {
      "Content-Type": "application/json",
    }).then((value) => value.body);

it gives instance of etc. But If I print this value.body, then it gives json object, so I can see the values.

Now, since I can not see the values without print, I can not use in my futurebuilder. I want to get these values without print. Help please.


Future Builder code : and,

final _postRequest = PostRequest()
      .postData(amount: state.amount.toInt(), maturity: state.maturity.toInt());

my _postRequest instance

 FutureBuilder(
                  future: _postRequest,
                  builder: (context, snapshot) {
                    print("ben snapshot datasıyım ${snapshot.data}");

                    if (!snapshot.hasData) {
                      return const Center(
                        child: CircularProgressIndicator(),
                      );
                    }
                    return Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        const Text(
                          "Your Results",
                          style: TextStyle(
                              fontSize: 30, fontWeight: FontWeight.w500),
                        ),
                     
                      ],
                    );
                  }),

enter image description here

{"id":null,"amount":1000,"created_at":null,"client_id":null,"type":"0","maturity":19,"carCondition":null,"total_offers":8,"offers":[{"bank_id":400100099,"bank":"ING","interest_rate":1.79,"sponsored_rate":0,"bank_type":"ozel","url":"https://www.ing.com.tr/tr/sizin-icin/krediler/ihtiyac-kredisi-basvuru-formu","hypothec_fee":0,"expertise":0,"annual_rate":29.57512030367764},{"bank_id":400100032,"bank":"TEB","interest_rate":1.85,"sponsored_rate":0,"bank_type":"ozel","url":"https://www.cepteteb.com.tr/cepteteb-kredi-basvurusu?utm_source=teklifimgelsin&utm_medium=affiliate&utm_campaign=kredi","hypothec_fee":0,"expertise":0,"annual_rate":30.670908509091646},{"bank_id":400200010,"bank":"Ziraat","interest_rate":1.94,"sponsored_rate":0,"bank_type":"kamu","url":"https://www.ziraatbank.com.tr/tr/bireysel/basvurular/bireysel-kredi-basvurulari/tuketici-kredisi","hypothec_fee":0,"expertise":0,"annual_rate":32.33059049829124}]}

Upvotes: 1

Views: 6156

Answers (2)

alperefesahin
alperefesahin

Reputation: 781

After "Whole" process, I found the solution in the DIO Package. Thanks to it. just do this:

    final response = await dio.post('/briefLoanOffer', data: {
      'amount': amount,
      'maturity': maturity,
      "type": type,
      "offer_count": offerCount
    });

    return response.data;
  }

That's it!

Upvotes: 0

Hardik Mehta
Hardik Mehta

Reputation: 2425

You can do this way :

var response = await http.post(url, body: jsonEncode(body), headers: {
      "Content-Type": "application/json",
    });

use this website for convert json to dart class : https://javiercbk.github.io/json_to_dart/

and then

if (response.statusCode == 200) {
            Classname classinstance =
            Classname.fromJson(jsonDecode(response.body));
   }

now you can access value from classinstance and like this classinstance.data

Upvotes: 0

Related Questions