Home > Enterprise >  I can't run an app with a real device in android studio
I can't run an app with a real device in android studio

Time:05-19

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related