-1

I'm following a tutorial on integrating a Go backend with Flutter from this YouTube video. Despite following the instructions closely, I'm unable to retrieve data from the backend into my Flutter application. The tutorial's author suggested printing the HTTP status code using response.statusCode to debug, but I'm unsure how to implement this in Go.

Context:

I'm relatively new to Go and working on a project that involves setting up a backend service for a Flutter app. The goal is to send and receive data between the Flutter frontend and the Go backend. While testing, I encountered issues with data retrieval and was advised to check the HTTP status code to diagnose the problem.

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() {
    // Code for initializing tasks omitted for brevity
}

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")
    json.NewEncoder(w).Encode(tasks)
}

// Other handlers omitted for brevity

func handleRoutes() {
    router := mux.NewRouter()
    // Routes setup omitted for brevity
    log.Fatal(http.ListenAndServe(":3306", router))
}

func main() {
    allTasks()
    handleRoutes()
}


What I've Tried:

  • I verified that the routes are correctly set up and that the server is running without errors.
  • I attempted to log the output in the Flutter app to check if the data is received but found no useful information.

Question:

How can I modify my Go code to print the HTTP status code when making requests? I believe understanding how to log these status codes correctly will help me debug the connectivity issue with my Flutter front end.

1 Answer 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. Tested with postman

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, it shows in Flutter: I/chatty (14409): uid=10128(com.example.flutter_golang_yt) 1.ui identical 1 line I/flutter (14409): null What should I do in that case?
To be honest I am not flutter developer but if you can provide the flutter code where this error is triggered let us try to find the solution
Hello rqb I post the code below

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.