2

how to pass the token value from above code to another screen into a http-request as variable.

class AppStarted extends AuthenticationEvent {}

class LoggedIn extends AuthenticationEvent {
  final String token;

  const LoggedIn({@required this.token});

  @override
  List<Object> get props => [token];

  @override
  String toString() => 'LoggedIn { token: $token }';

}

Thank you for your help, haning on this for 2 hours now...

5
  • Please post the other half of your code, where you want to pass or extract thee token. Commented Apr 3, 2021 at 12:49
  • can you give more details of your code ? Commented Apr 3, 2021 at 13:15
  • @BigWatanga On other page i do a simple http request where i need the token variable. The Problem is, that the code is to much to share. Commented Apr 3, 2021 at 14:09
  • @HuthaifaMuayyad On other page i do a simple http request where i need the token variable. The Problem is, that the code is to much to share. Commented Apr 3, 2021 at 14:09
  • What we meant, are you capturing the token in the other page? and trying to send it here? We dodn't need your token, nobody is interested in the secret token, but we have to know the method you are persuing in order to help you debug your prolblem. Commented Apr 3, 2021 at 14:37

1 Answer 1

1

first create a class where your where https request handle & token store

class Network {
      var  token;
    final string base= "/api";
     //token get when needed 
 _getToken() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
     var user = jsonDecode(localStorage.getString('data'));
     token = user['token'];
     }

    //signup 
  signUp(data, apiUrl) async {
    var fullUrl = baseUrl + apiUrl;
    return await http.post(fullUrl,
    body: jsonEncode(data), headers: _setHeaders());
    }

  //login
   signIn(apiUrl) async {
   var fullUrl = baseUrl + apiUrl;
    await _getToken();
     return await http.get(fullUrl, headers: _setHeaders());
   }

  }

in sign up page use this method where you use sign up button on pressed

void _signUp() async {

var data = {
  'mobile': mobileController.text,
  'password':  mobileController.text,
};

var res = await Network().signUp(data, '/register');
var body = json.decode(res.body);
if (body.statusCode == 200) {
  SharedPreferences localStorage = await SharedPreferences.getInstance();
  localStorage.setString('token', json.encode(body['data']['token']));

  localStorage.setString('data', json.encode(body['data']));

  Navigator.push(
    context,
    new MaterialPageRoute(builder: (context) => Home()),
  );
} else  {
//      errorMessage = "as you want to show ";

 
}


  }

in sign up page , use this method same as sign up pages

void signIn() async {

var data = {'mobile': mobile, 'password': password};
var res = await Network().signIn(data, '/login');
var body = json.decode(res.body);

if (res.statusCode == 200) {
  SharedPreferences localStorage = await SharedPreferences.getInstance();
  localStorage.setString('token', json.encode(body['data']['token']));
  localStorage.setString('data', json.encode(body['data']));
  Navigator.pushReplacement(
    context,
    new MaterialPageRoute(builder: (context) => HomePage()),
  );
} else if (res.statusCode != 200) {
 // mobileError = "These credentials do not match our records.";
}
   }

now you can have the token when u signup/singIn and store token in sharedSharedPreferences whenever you try to acces token just called _getToken() method

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.