0

Hello everyone hope you are great, I run the app on an emulator device in android studio, and it works fine, but with a real device I can't, it shows “we did not get any data”, tell me what do you need to solve this problem, because a have a lot of code and don't know what to show you and most importantly what file of code matters to fix this error, so please tell me if you need something and I sent to you.

I tried to run the app in a Samsung s9

this is my service.dart

import 'package:flutter/foundation.dart';
import 'package:flutter_golang_yt/colors/app_constants.dart';
import 'package:get/get.dart';

class DataService extends GetConnect implements GetxService{

  Future<Response> getData(String uri)async{
    if (kDebugMode) {
      print(AppConstants.BASE_URL+uri);
    }
    Response response=await get(
      AppConstants.BASE_URL+uri,
      headers: {
        'Content-Type':'application/json; charset=UTF-8'
      }
    );
    return response;
  }

  Future<Response> postData(String uri, dynamic body)async{
    Response response=await post(
        AppConstants.BASE_URL+uri,
        body,
        headers: {
          'Content-Type':'application/json; charset=UTF-8'
        }
    );
    return response;
  }

  Future<Response> updateData(String uri, dynamic body)async{
    Response response=await put(
        AppConstants.BASE_URL+uri,
        body,
        headers: {
          'Content-Type':'application/json; charset=UTF-8'
        }
    );
    return response;
  }

  Future<Response> deleteData(String uri)async{
    Response response=await delete(
        AppConstants.BASE_URL+uri,
        headers: {
          'Content-Type':'application/json; charset=UTF-8'
        }
    );
    return response;
  }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flutter_golang_yt">

   <application
        android:label="Todogo"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

data_controller.dart

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter_golang_yt/colors/app_constants.dart';
import 'package:get/get.dart';
import '../services/service.dart';

class DataController extends GetxController{
  DataService service = DataService();
  bool _isLoading = false;
  bool get isLoading => _isLoading;
  List<dynamic> _myData=[];
  List<dynamic> get myData=>_myData;
  Map<String, dynamic> _singleData={};
  Map<String, dynamic> get singleData => _singleData;

  Future<void> getData() async {
    _isLoading = true;
    Response response = await service.getData(AppConstants.GET_TASKS);
    if(response.statusCode==200){
      _myData=response.body;
      print("we got the data");
      update();
    }else{
      print("we did not get any data");
    }
    _isLoading = false;
    update();
  }

  Future<void> getSingleData(String id) async {
    _isLoading = true;
    Response response = await service.getData('${AppConstants.GET_TASK}''?id=$id');

    if(response.statusCode==200){

      if (kDebugMode) {

        print("we got the single data"+jsonEncode(response.body));
        _singleData=response.body;
      }
    }else{
      print("we could not get the data");
    }
    _isLoading = false;
    update();
  }

  Future<void> postData(String task, String taskDetail) async {
    _isLoading = true;
    Response response = await service.postData(AppConstants.POST_TASK,{
      "task_name":task,
      "task_detail":taskDetail
    });
    if(response.statusCode==200){
      print("data post successfully");
    }else{
      print("data post failed");
    }
    update();
  }
  Future<void> updateData(String task, String taskDetail, int id) async {
    _isLoading = true;
    Response response = await service.updateData('${AppConstants.UPDATE_TASK}''?id=$id',{
      "task_name":task,
      "task_detail":taskDetail
    });
    if(response.statusCode==200){
      print("data post successfully");
    }else{
      print("data post failed");
    }
    update();
  }
  Future<void> deleteData(int id) async {
    _isLoading = true;
    update();
    Response response = await service.deleteData('${AppConstants.DELETE_TASK}''?id=$id');
    if(response.statusCode==200){
      print("data post successfully");
    }else{
      print("data post failed");
    }
    await Future.delayed(Duration(seconds: 1),
        (){
          _isLoading = false;
          update();
        }
    );
  }
}

app_constants.dart

class AppConstants{
  static const String BASE_URL="http://10.0.2.2:8080";
  static const String GET_TASKS="/gettasks";
  static const String POST_TASK="/create";
  static const String GET_TASK="/gettask/";
  static const String UPDATE_TASK="/update/";
  static const String DELETE_TASK="/delete/";
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
lomipac
  • 571
  • 8
  • 18

2 Answers2

2

10.0.2.2 is a special alias to your host loopback interface i.e., 127.0.0.1 on your development machine. You may be able to change your AppConstants to your LAN address if your device and backend are running on the same LAN. This may require additional configuration to get it to work.

class AppConstants{
  static const String BASE_URL="http://192.168.xx.xx:8080";
  static const String GET_TASKS="/gettasks";
  static const String POST_TASK="/create";
  static const String GET_TASK="/gettask/";
  static const String UPDATE_TASK="/update/";
  static const String DELETE_TASK="/delete/";
}

You may use ifconfig -a to get your LAN IP.

void void
  • 1,080
  • 3
  • 8
  • 1
    More info here - https://stackoverflow.com/questions/9808560/why-do-we-use-10-0-2-2-to-connect-to-local-web-server-instead-of-using-computer – Lee Taylor May 18 '22 at 22:49
  • I changed the IP to IPv4 Address and it doesn't work – lomipac May 18 '22 at 22:53
  • What do you mean with that? What did you set BASE_URL to? – void void May 18 '22 at 22:54
  • I set this: static const String BASE_URL="http://192.168.0.4:8080"; – lomipac May 18 '22 at 23:04
  • Test if your webapp is accessible with that BASE_URL on your phone. Open it with a web browser on your device – void void May 18 '22 at 23:09
  • How can I do it? Step by step, please. – lomipac May 18 '22 at 23:16
  • Enter http://192.168.0.4:8080 in you phone's browser's (for example Chrome) address bar and press enter. If it's not accessible then it might be blocked by a firewall or similar, but that's another question. Check https://stackoverflow.com/questions/5489956/how-could-others-on-a-local-network-access-my-nodejs-app-while-its-running-on for a similar issue. – void void May 18 '22 at 23:18
  • it's not accessible, what I have to do in that case? – lomipac May 18 '22 at 23:29
  • That's probably the ip address of your router, not of your machine that is running the backend. You deleted your previous comment so the conversation now is misleading. Check the similar issue i posted in the previous comment and ask a separate question if you still can't get it to work because it's a separate issue. – void void May 18 '22 at 23:31
  • Even if I tried to run the app by USB connection? – lomipac May 19 '22 at 01:15
1

Based on static const String BASE_URL="http://192.168.xx.xx:8080" requests which are outside SSL Encryption, add:

<application android:usesCleartextTraffic="true">
</application>

To the AndroidManifest.xml

Chris Pi
  • 536
  • 1
  • 5
  • 21