0

So I have created a login screen that posts the user's information and then returns a token which is then store in their SharedPreferences. How can I now use this token to with another request to display the information that is associated with their profile.

I believe I have to create another post request. This time with slightly different headers and include the token in the header. Then parse the response into usable data?

1 Answers1

0

Try this!

  Map<String, dynamic> parameterData;
  Map<String, dynamic> responseMap = new Map();
  String url = Host.baseURL + "API_URL";
  parameterData.putIfAbsent("user_name", () => "ABC");

  String data = json.encode(parameterData);

  try {
    await http.post(Uri.encodeFull(url),
        headers: {"Accept": "application/json", "auth": "put_acess_token_here"},
        body: {"data": data}).catchError((onError) {
    }).then((response) {
      if (response != null) {
        responseMap = json.decode(response.body);
      }
    });
  } catch (exception, stackTrace) {
    printLog(exception);
    printLog(stackTrace);
  }

You need to import below

import 'package:http/http.dart' as http;
Ajay Kumar
  • 15,250
  • 14
  • 54
  • 53