I’m facing challenges while trying to connect my Flutter application to an API on RapidAPI. Despite my efforts to resolve the issue, I haven’t had any success. I’d be grateful for assistance in fetching this data into my Flutter app. Below is the code I’ve implemented.
```dart
import ‘dart:convert’;
import ‘package:api_test/Property.dart’;
import ‘package:http/http.dart’ as http;
class ApiHandler {
Future<List?> fetchProperties() async {
var url = Uri.https(‘bayut.p.rapidapi.com’, ‘/properties/detail’,
{‘externalID’: ‘4937770’});
var response = await http.get(url, 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('Error loading data');
}
}
}
This is the model I'm using:<br><br>```dart
class Property {
final String name;
final String info;
final String type;
final String frequency;
final String imageUrl;
final String mainImage;
final String phone;
final String agentName;
final String companyName;
final String logoUrl;
final String furnishing;
final int virtualTour;
final int imagesCount;
final int videoCount;
final int cost;
Property({required this.mainImage, required this.info, required this.name, required this.type, required this.frequency, required this.imageUrl, required this.phone, required this.agentName, required this.companyName, required this.logoUrl, required this.furnishing, required this.virtualTour, required this.imagesCount, required this.videoCount, required this.cost});
factory Property.fromJson(Map<String, dynamic> json) {
return Property(
name: json['title'] as String,
type: json['purpose'] as String,
frequency: json['rentFrequency'] as String,
mainImage: json['coverPhoto']['url'] as String,
imageUrl: json['photos'][0]['url'] as String,
phone: json['phoneNumber']['mobile'] as String,
agentName: json['contactName'] as String,
companyName: json['agency']['name'] as String,
logoUrl: json['agency']['logo']['url'] as String,
furnishing: json['furnishingStatus'] as String,
virtualTour: json['panoramaCount'] as int,
imagesCount: json['photoCount'] as int,
videoCount: json['videoCount'] as int,
cost: json['price'] as int,
info: json['description'] as String,
);
}
}
The following illustrates where I invoke this method:
```dart
import ‘package:api_test/Property.dart’;
import ‘package:api_test/api_handler.dart’;
import ‘package:flutter/material.dart’;
class PropertyScreen extends StatefulWidget {
const PropertyScreen({Key? key}) : super(key: key);
@override
State createState() => _PropertyScreenState();
}
class _PropertyScreenState extends State {
List? _propertyList;
bool _isDataFetchComplete = false;
@override
void initState() {
super.initState();
retrieveProperties();
}
Future retrieveProperties() async {
_propertyList = await ApiHandler().fetchProperties();
if (_propertyList != null) {
setState(() {
_isDataFetchComplete = true;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Visibility(
visible: _isDataFetchComplete,
child: ListView.builder(
itemCount: _propertyList?.length,
itemBuilder: (context, index) {
return Container(
child: Text(‘Hello’),
);
}),
replacement: Center(
child: CircularProgressIndicator(),
),
));
}
}
Currently, the loading spinner keeps spinning indefinitely, indicating that there is a connection error.