'How to print statuscode using response.statusCode in Visual Studio Code using Go language
I'm currently doing this tutorial from YouTube : https://www.youtube.com/watch?v=-Sol_RMG_fo&ab_channel=dbestech in the tutorial I can't get the data from back end to flutter, so I asked the YouTuber what should I do?, and he tells me to print statuscode using response.statusCode, but really don't know what I have to do Here's the code:
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
type Tasks struct {
ID string `json:"id"`
TaskName string `json:"task_name"`
TaskDetail string `json:"detail"`
Date string `json:"date"`
}
var tasks []Tasks
func allTasks() {
task := Tasks{
ID: "1",
TaskName: "New Projects",
TaskDetail: "You must lead the project and finish it",
Date: "2022-01-22"}
tasks = append(tasks, task)
task1 := Tasks{
ID: "2",
TaskName: "Power Project",
TaskDetail: "We need to hire more stuffs before the deadline",
Date: "2022-01-22"}
tasks = append(tasks, task1)
fmt.Println("your tasks are", tasks)
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Println("I am home page")
}
func getTasks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/")
json.NewEncoder(w).Encode(tasks)
}
func getTask(w http.ResponseWriter, r *http.Request) {
taskId := mux.Vars(r)
flag := false
for i := 0; i < len(tasks); i++ {
if taskId["id"] == tasks[i].ID {
json.NewEncoder(w).Encode(tasks[i])
flag = true
break
}
}
if flag == false {
json.NewEncoder(w).Encode(map[string]string{"status": "Error"})
}
}
func createTask(w http.ResponseWriter, r *http.Request) {
fmt.Println("I am home page")
}
func deleteTask(w http.ResponseWriter, r *http.Request) {
fmt.Println("I am home page")
}
func updateTask(w http.ResponseWriter, r *http.Request) {
fmt.Println("I am home page")
}
func handleRoutes() {
router := mux.NewRouter()
router.HandleFunc("/", homePage).Methods("GET")
router.HandleFunc("/gettasks", getTasks).Methods("GET")
router.HandleFunc("/gettask/{id}", getTask).Methods("GET")
router.HandleFunc("/create", createTask).Methods("POST")
router.HandleFunc("/delete/{id}", deleteTask).Methods("DELETE")
router.HandleFunc("/update/{id}", updateTask).Methods("PUT")
log.Fatal(http.ListenAndServe(":3306", router))
}
func main() {
allTasks()
fmt.Println("Hello Flutter boys")
handleRoutes()
}
Solution 1:[1]
I think he said you to print the statuscode from flutter side. Then based on that statuscode you can understand where was the issue.
I have tested your code in local using postman and I am getting data.

Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | lomipac |
