Home > Blockchain >  How to print statuscode using response.statusCode in Visual Studio Code using Go language
How to print statuscode using response.statusCode in Visual Studio Code using Go language

Time:05-13

I'm currently doing this tutorial from YouTube : Tested with postman

CodePudding user response:

Hello @rqb here's the code from flutter

data_controller.dart:

import 'package:flutter_golang_yt/services/service.dart';
import 'package:get/get.dart';

class DataController extends GetxController{
  DataService service = DataService();
  bool _isLoading = false;
  bool get isLoading=> _isLoading;
  List<dynamic> _myData=[];
  List<dynamic> get myData=>_myData;
  Future<void> getData() async {
    _isLoading = true;
    Response response = await service.getData();
    if(response.statusCode==200){
      _myData=response.body;
      print("we got the data");
      update();
    }else{
      print(response.statusCode.toString());
    }
  }

  Future<void> postData(String task, String taskDetail) async {
    _isLoading = true;
    Response response = await service.postData({
      "task_name":task,
      "task_detail": taskDetail
    });
    if(response.statusCode==200){

      update();
      print("data post successfully");
    }else{
      print("data post failed");
    }
  }

}

And services.dart:

import 'package:get/get.dart';

class DataService extends GetConnect implements GetxService{

  Future<Response> getData()async{
    Response response=await get(
      "http://localhost:3306/gettasks",
      headers: {
        'Content-Type':'application/json; charset=UTF-8'
      }
    );

    return response;
  }

  Future<Response> postData(dynamic body)async{
    Response response=await post(
        "http://localhost:3306/create",
        body,
        headers: {
          'Content-Type':'application/json; charset=UTF-8'
        }
    );
    return response;
  }
}

It shows on terminal:

[GETX] Instance "DataController" has been created
[GETX] Instance "DataController" has been initialized
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
I/flutter (15548): null
  • Related