0

I tried connecting to an API on rapidapi but it isn't connecting, I've tried everything possible and to no avail. any help in getting this data from rapid api into flutter will be much appreciated. here is the code.

import 'dart:convert';

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

class APIService {
/*const req = unirest("GET", "https://bayut.p.rapidapi.com/properties/detail");

req.query({
    "externalID": "4937770"
});

req.headers({
    "X-RapidAPI-Host": "bayut.p.rapidapi.com",
    "X-RapidAPI-Key": "**************************************",
    "useQueryString": true
});*/

  Future<List<Properties>?> getProperties() async {
    //{required String endpoint, required Map<String, String> query}) async {
    var uri = Uri.https('bayut.p.rapidapi.com', '/properties/detail',
        {"externalID": "4937770"});

    var response = await http.get(uri, headers: {
      "X-RapidAPI-Host": "bayut.p.rapidapi.com",
      "X-RapidAPI-Key": "**************************************",
      "useQueryString": "true"
    });
    if (response.statusCode == 200) {
      return (json.decode(response.body));
    } else {
      throw Exception('Failed to load Data');
    }
  }
}

this is the model

class Properties {
  final String title;
  final String description;
  final String purpose;
  final String rentFrequency;
  final String image;
  final String coverPhoto;
  final String mobileNumber;
  final String contactName;
  final String agencyName;
  final String agencyLogo;
  final String furnishingStatus;
  final int panoramaCount;
  final int photoCount;
  final int videoCount;
  final int price;

  Properties(
      {required this.coverPhoto,
      required this.description,
      required this.title,
      required this.purpose,
      required this.rentFrequency,
      required this.image,
      required this.mobileNumber,
      required this.contactName,
      required this.agencyName,
      required this.agencyLogo,
      required this.furnishingStatus,
      required this.panoramaCount,
      required this.photoCount,
      required this.videoCount,
      required this.price});

  factory Properties.fromjson(dynamic json) {
    return Properties(
      title: json['title'] as String,
      purpose: json['purpose'] as String,
      rentFrequency: json['rentFrequency'] as String,
      coverPhoto: json['coverPhoto']['url'] as String,
      image: json['photos'][0]['url'] as String,
      mobileNumber: json['phoneNumber']['mobile'] as String,
      contactName: json['contactName'] as String,
      agencyName: json['agency']['name'] as String,
      agencyLogo: json['agency']['logo']['url'] as String,
      furnishingStatus: json['furnishingStatus'] as String,
      panoramaCount: json['panoramaCount'] as int,
      photoCount: json['photoCount'] as int,
      videoCount: json['videoCount'] as int,
      price: json['price'] as int,
      description: json['description'] as String,
    );
  }

  static List<Properties> propertiesfromsnapshot(List snapshot) {
    return snapshot.map((data) {
      return Properties.fromjson(data);
    }).toList();
  }
}

here is where I called it

import 'dart:ffi';

import 'package:api_test/Property.dart';
import 'package:api_test/services.dart';
import 'package:flutter/material.dart';

class PropertyPage extends StatefulWidget {
  const PropertyPage({Key? key}) : super(key: key);

  @override
  State<PropertyPage> createState() => _PropertyPageState();
}

class _PropertyPageState extends State<PropertyPage> {
  List<Properties>? _properties;
  bool _isLoaded = false;
  @override
  void initstate() {
    super.initState();
    getProperties();
  }

  Future<void> getProperties() async {
    _properties = await APIService().getProperties();
    if (_properties != null) {
      setState(() {
        _isLoaded = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Visibility(
      visible: _isLoaded,
      child: ListView.builder(
          itemCount: _properties?.length,
          itemBuilder: ((context, index) {
            return Container(
              child: Text("Hi"),
            );
          })),
      replacement: Center(
        child: CircularProgressIndicator(),
      ),
    ));
  }
}

the circular progress bar runs infinitely, meaning there is an error in the connection

1 Answers1

0

This code can be of help to you:

import 'package:wnetworking/wnetworking.dart';

class Bayut {
  static const _url = 'https://bayut.p.rapidapi.com';
  static const _apiKey = '1111111111111111111111111111111';
  
  Bayut._();

  static Future<void> getPropertiesDetail() async {
    var result = await HttpReqService.get<JMap>(
      '$_url/properties/detail?externalID=4937770',
      auth: AuthType.apiKey,
      authData: MapEntry('X-RapidAPI-Key', _apiKey),
      headers: {'X-RapidAPI-Host': 'bayut.p.rapidapi.com'}
    );

    print(result?.keys);
  }
}

void main(List<String> args) async {
  await Bayut.getPropertiesDetail();
  print('\nJob done!');
}

Output:

(id, objectID, ownerID, ..., indyScore_l1, hasMatchingFloorPlans)

Job done!

And use FutureBuilder instead of ListBuilder.