Last active
February 5, 2020 03:27
-
-
Save godilite/196bb9f9ccb5cdc845741c6c1452e468 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
class Network{ | |
final String _url = 'http://localhost:8000/api/v1'; | |
//if you are using android studio emulator, change localhost to 10.0.2.2 | |
var token; | |
_getToken() async { | |
SharedPreferences localStorage = await SharedPreferences.getInstance(); | |
token = jsonDecode(localStorage.getString('token'))['token']; | |
} | |
authData(data, apiUrl) async { | |
var fullUrl = _url + apiUrl; | |
return await http.post( | |
fullUrl, | |
body: jsonEncode(data), | |
headers: _setHeaders() | |
); | |
} | |
getData(apiUrl) async { | |
var fullUrl = _url + apiUrl; | |
await _getToken(); | |
return await http.get( | |
fullUrl, | |
headers: _setHeaders() | |
); | |
} | |
_setHeaders() => { | |
'Content-type' : 'application/json', | |
'Accept' : 'application/json', | |
'Authorization' : 'Bearer $token' | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment