im trying to pass parameters into a get request and i am having problems. i try to do it manually as seen in the code but it returns error. please help.
this is my service file
import 'dart:convert';
import 'package:http/http.dart';
import '../models/journal_response_model.dart';
class GetJournalService {
String endpoint =
'https://friendnpalapp-production-fbcc.up.railway.app/api/journal';
Future<List<JournalResponseModel>> getArticles(String username) async {
Response response =
await get(Uri.parse('${endpoint}' '?username=$username'));
if (response.statusCode == 200) {
final List result = [jsonDecode(response.body)];
print(response.body);
return result.map(((e) => JournalResponseModel.fromJson(e))).toList();
} else {
print('error');
throw Exception(response.reasonPhrase);
}
}
}
this is my future builder
FutureBuilder<List<JournalResponseModel>>(
future: GetJournalService().getArticles('jack'),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
print(snapshot.data!);
final journal = snapshot.data![index];
return EntryWidget(
backgroundColor: AppColors.blue,
icon: Icons.remove,
mood: journal.mood!,
date: journal.createdAt!,
);
},
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return CircularProgressIndicator();
})
2
Answers
Here’s a slightly modified official example from DartPad
This code prints out
GET https://www.googleapis.com/books/v1/volumes?q=http
In your case, the url should be something like (just replace Test with the username):
This question has been resolved. The cause was an api error.
(Return 404 error – this indicates that the api has an error.)