Abdul Rahman Panhyar
Abdul Rahman Panhyar

Reputation: 198

how can I fetch data from flutter api with parameters as body/raw data

**

  1. Post man Code

    **curl --location --request POST 'http://testing.thedivor.com/api/API/GetDistance' --header 'Content-Type: application/json' --data-raw '[ { "id": "", "placeid": "EiBMZXdpc2hhbSBXYXksIExvbmRvbiBTRTQgMVVZLCBVSyIuKiwKFAoSCXk1YsdYAnZIERCJXqRiE8WPEhQKEgnrjwApXwJ2SBH76fXJO6C02w", "address": "Lewisham Way, London SE4 1UY, UK", "postcode": "SE4 1UY", "outcode": "SE4 1UY", "lattitude": 51.4702816, "country": "United Kingdom", "city": "Greater London", "longitude": -0.029187800000045172 }, { "_id": "null", "placeid": "", "address": "Kenley Road, London SW19 3DW, UK", "postcode": "SE23 3RF", "outcode": "SE23", "lattitude": "51.404556", "country": "", "city": "", "longitude": "-0.194486" } ]'

Flutter Code

'''var req = await Requests.post('http://testing.thedivor.com/api/API/GetDistance?', headers: {
      'Content-Type' :  'application/json'
    }, 

body: body // json objects );

    print(req.json());
    print(req.content().length);'''

Upvotes: 0

Views: 642

Answers (1)

Thierry
Thierry

Reputation: 8383

enter image description here

Try this:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: HomePage(),
    ),
  );
}

class HomePage extends HookWidget {
  Future<String> _getDistance() async {
    final response = await http.post(
      'http://testing.thedivor.com/api/API/GetDistance',
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      },
      body: jsonEncode(params),
    );
    print(response.body);
    return response.statusCode == 200
        ? jsonDecode(response.body).toString()
        : 'Failed to get distance';
  }

  @override
  Widget build(BuildContext context) {
    final _distance = useState<String>('');
    useEffect(() {
      _getDistance().then((d) => _distance.value = d);
      return;
    }, []);
    return Scaffold(
      body: Center(
        child: Text(_distance.value),
      ),
    );
  }
}

final List<Map<String, dynamic>> params = [
  {
    "id": "",
    "placeid":
        "EiBMZXdpc2hhbSBXYXksIExvbmRvbiBTRTQgMVVZLCBVSyIuKiwKFAoSCXk1YsdYAnZIERCJXqRiE8WPEhQKEgnrjwApXwJ2SBH76fXJO6C02w",
    "address": "Lewisham Way, London SE4 1UY, UK",
    "postcode": "SE4 1UY",
    "outcode": "SE4 1UY",
    "lattitude": 51.4702816,
    "country": "United Kingdom",
    "city": "Greater London",
    "longitude": -0.029187800000045172,
  },
  {
    "_id": "null",
    "placeid": "",
    "address": "Kenley Road, London SW19 3DW, UK",
    "postcode": "SE23 3RF",
    "outcode": "SE23",
    "lattitude": "51.404556",
    "country": "",
    "city": "",
    "longitude": "-0.194486",
  },
];

Upvotes: 1

Related Questions