I’m facing difficulties retrieving data from RapidAPI in my Flutter application. Despite several attempts, the connection to the API doesn’t seem to work properly. To address this issue, I built an API handler class as shown below:
class DataFetcher {
Future<List<RealEstate>?> retrieveListings() async {
var url = Uri.https('realestate-api.example.com', '/listings/details', {
'listingId': '12345'
});
var result = await http.get(url, headers: {
'API-Host': 'realestate-api.example.com',
'API-Key': 'your-api-key-here',
'UseQueryString': 'true'
});
if (result.statusCode == 200) {
return parseRealEstate(result.body);
} else {
throw Exception('Data retrieval failed');
}
}
}
I also implemented a model called RealEstate to parse the JSON response. Additionally, I created a stateful widget for displaying the data:
class ListingsScreen extends StatefulWidget {
@override
_ListingsScreenState createState() => _ListingsScreenState();
}
class _ListingsScreenState extends State<ListingsScreen> {
List<RealEstate>? listings;
bool loading = true;
@override
void initState() {
super.initState();
loadData();
}
Future<void> loadData() async {
try {
listings = await DataFetcher().retrieveListings();
setState(() => loading = false);
} catch (error) {
print('Error: $error');
setState(() => loading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: loading
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: listings?.length ?? 0,
itemBuilder: (context, index) {
return Text(listings![index].title);
},
),
);
}
}
Even though I set up the progress indicator to display during loading, it continues spinning without stopping. Does anyone have suggestions on what might be causing this issue or how to effectively debug the connection?