1

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)),
                  ),
                ],
              ),
            ),
          )
        );
      }
}

1 Answer 1

1

Let me correct you,

  1. You can't send body with GET http request, so change the API to POST or send id_machdien as Query parameter.
  2. Inside class Get You are trying to parse json['id_machdien'] but in your response there is no id_machdien, it must be one of the response's item (like : id, trang_thai_app).
Sign up to request clarification or add additional context in comments.

3 Comments

i wan't to get data on sql server through my flutter api not post, and how i send '''id_machdien''' as Query parameter? Can you write it down, i not good at this. In my response there is no id_machdien(so how can i fix this), and yes it's the one for the response's item (like : id, trang_thai_app)
You need to make some changes to your API too, Can you share the code of your backend?

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.