My http-get json data in flutter project it can't get the data, I've tested with postman and it's succeed, but in flutter it's didn't or because of me don't know how to put in. I've try different ways but none are succeed!
Can anyone help me?! If anyone need more info, I'll give it to you!
Flutter Debug Console: https://i.sstatic.net/IGZDP.png
Postman tested succeed: https://i.sstatic.net/khj9a.png
Postman tested failed: https://i.sstatic.net/Ee6tU.png
Future<Get> getdata(String id) async {
final username = 'test';
final password = '9876543';
final credentials = '$username:$password';
final stringToBase64 = utf8.fuse(base64);
final encodedCredentials = stringToBase64.encode(credentials);
final String apiUrl = "http://sv.com/api/values/";
Map<String, String> headers = {
HttpHeaders.contentTypeHeader: "application/json; charset=utf-8",
HttpHeaders.authorizationHeader: "Basic $encodedCredentials",
};
final response = await http.get(apiUrl,headers: headers);
if (response.statusCode == 200) {
final String responseString = response.body;
print(response.statusCode);
print(responseString);
return Get.fromJson(json.decode(response.body));
} else {
print(response.statusCode);
print(response.body);
}
}
Get class
class Get {
final String id;
Get({
this.id,
});
factory Get.fromJson(Map<String, dynamic> json) {
return Get(
id: json['id_machdien'].toString()
);
}
}
Main
class _MyHomePageState extends State<MyHomePage> {
Get _dataget;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body:Center(
child: Column(
children: <Widget> [
RaisedButton(
onPressed: () async{
final Get dataget = await getdata(id);
setState(() {
_dataget = dataget;
});
},
child: Text('Mở',style: TextStyle(fontSize: 15)),
),
],
),
),
)
);
}
}