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, so I did it:
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()); // I replaced "print(we did not get any data);" because in the tutorial is what it showed, but on my terminal showed me "we did not get any data" and on YouTuber's terminal it showed "we got the 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");
}
}
}
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
I also have the code from back end using Go in Visual Studio 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")
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()
}
CodePudding user response:
Try some simple additional debugging statements like:
Future<void> getData() async {
_isLoading = true; // todo - if UI depends on this do this inside setState
print('calling getData');
final response = await service.getData();
print('got response - checking it...')
if (response.statusCode == 200) {
_myData = response.body;
print('we got the data');
print(response.body);
update();
print('finished updating');
} else {
print(response.statusCode);
}
}
CodePudding user response:
The first thing you want to do is determine whether it's "response" or "response.statusCode" which is null. (print them out)
Once you've determined that, you'll want to look at the documentation for "get" which will tell you the situations in which it that thing can be null.
Also, if you're following this tutorial without much coding knowledge, I'd recommend starting with something simpler. Playing with asynchronous code and big 3rd party libraries is not a great starting place.