skip to Main Content

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


  1. Here’s a slightly modified official example from DartPad

    import 'package:http/http.dart' as http;
    
    void main(List<String> arguments) async {
      final url = Uri.https(
        'www.googleapis.com'
        '/books/v1/volumes',
        {'q': 'http'},
      );
    
      final response = await http.get(url);
      print(response.request.toString());
    }
    

    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):

    final url = Uri.https(
      'friendnpalapp-production-fbcc.up.railway.app',
      '/api/journal',
      {'username': 'Test'},
    );
    
    Login or Signup to reply.
  2. This question has been resolved. The cause was an api error.

    (Return 404 error – this indicates that the api has an error.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search