'Golang backend problems with Flutter trying to get the data

I'm making a to-do list app and can't get the data from backend using Golang from Visual Studio Code.

Here's the code from the back end (Golang):

package main

import (
    "fmt"
    "log"
    "net/http"
    "github.com/gorilla/mux"
    "strconv"
    "math/rand"
    "encoding/json"
)
type Tasks struct{
    ID string `json:"id"`
    TaskName string `json:"task_name"`
    TaskDetail string `json:"task_detail"`
    Date string `json:"date"`
}



var tasks []Tasks

func homePage(w http.ResponseWriter, r *http.Request){
    json.NewEncoder(w).Encode("Home Page")
}
func getTasks(w http.ResponseWriter, r *http.Request){
    w.Header().Set("Content-Type", "application/json")
    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){
    w.Header().Set("Content-Type", "application/json")
    var task Tasks
    _ = json.NewDecoder(r.Body).Decode(&task)
    task.ID = strconv.Itoa(rand.Intn(1000))
    tasks = append(tasks, task)
    json.NewEncoder(w).Encode(task)
}
func deleteTask(w http.ResponseWriter, r *http.Request){
    
}
func updateTask(w http.ResponseWriter, r *http.Request){
    
}


func handleRoute() {
    route := mux.NewRouter()
    route.HandleFunc("/", homePage).Methods("GET")
    route.HandleFunc("/gettasks", getTasks).Methods("GET")
    route.HandleFunc("/gettask/{id}", getTask).Methods("GET")
    route.HandleFunc("/create", createTask).Methods("POST")
    route.HandleFunc("/delete/{id}", deleteTask).Methods("DELETE")
    route.HandleFunc("/update/{id}", updateTask).Methods("PUT")

    log.Fatal(http.ListenAndServe(":3306", route))
}
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 main() {
    
    allTasks()
    fmt.Println("Hello Flutter boys")
    handleRoute()
}

Here's the code from Flutter, when I tried to collect the data from Visual Studio Code to run in the app:

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("we did not get any data");
    }
  }

  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");
    }
  }

}

--When I run the app, it shows me this from terminal in flutter:

I/flutter ( 6584): we did not get any data



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source