flutter - 我无法在 android studio 中使用真实设备运行应用程序

大家好,希望你们很棒,我在android studio的模拟器设备上运行该应用程序,它工作正常,但在真实设备上我不能,它显示“我们没有得到任何数据”,告诉我你怎么了需要解决这个问题,因为有很多代码并且不知道向您展示什么,最重要的是哪个代码文件对修复这个错误很重要,所以如果您需要一些东西,请告诉我,我发送给您。

我试图在三星 s9 中运行该应用程序

这是我的服务。飞镖

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

回答1

10.0.2.2 是主机环回接口的特殊别名,即开发机器上的 127.0.0.1。如果您的设备和后端在同一个 LAN 上运行,您可以将 AppConstants 更改为您的 LAN 地址。这可能需要额外的配置才能使其工作。

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

您可以使用 ifconfig -a 来获取您的 LAN IP。

回答2

基于 SSL 加密之外的 static const String BASE_URL="http://192.168.xx.xx:8080" 请求,添加:

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

AndroidManifest.xml

相似文章