【问题标题】:flutter - android - http.get never returns in release mode颤振 - android - http.get 在发布模式下永远不会返回
【发布时间】:2021-01-15 02:54:39
【问题描述】:

我正在为 android 和 ios 编写一个颤振应用程序。要登录,应用程序使用 http package(版本 0.12.2):http.Response response = await http.get(url);。 在 iOS 中,一切正常。在 Android 中,调试模式也可以正常工作,但是,如果我构建 apk 并从此版本 apk 安装应用程序,则异步方法 http.get 永远不会返回。 我在 OnePlus 3 (OxygenOS 9.0.6) 上使用 Android 9。

我对颤振很陌生,无法弄清楚这一点。 类似的issue 在 github 中开放,但担心 web 的颤动。

这里是重现的最小代码。 要测试它,您应该构建 apk (Build > Flutter > Build apk)),将 apk 复制并粘贴到您的手机文件中,从 apk 安装应用程序,按下按钮 PRESS MEdone 将永远不会显示.

  • main.dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Test flutter'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _status = "Press the button";

  void _test() async {
    String url = "http://dummy.restapiexample.com/api/v1/employees";

    // Send HTTP request to the server
    http.Response response = await http.get(
        url
    );

    setState(() {
      _status = "done";
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // Test button
            RaisedButton(
                onPressed: _test,
                child: Text(
                  "PRESS ME",
                ),
              ),
            Text(
              '$_status',
            ),
          ],
        ),
      ),
    );
  }
}
  • pubspec.yaml
name: flutter_test_app
description: Test app for reproducing minimal example

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  http: ^0.12.2

  cupertino_icons: ^0.1.3

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

【问题讨论】:

    标签: android flutter http


    【解决方案1】:

    您好,您需要向 AndroidManifest.xml 添加网络权限 查看有关颤振网络的官方文档 https://flutter.dev/docs/development/data-and-backend/networking

    它位于android/src/main/AndroidManifest.xml

    <manifest xmlns:android...>
     ...
     <uses-permission android:name="android.permission.INTERNET" />
     <application ...
    </manifest>
    

    【讨论】:

    • 我已经添加了 INTERNET 权限,但问题仍然存在,我遇到了问题中描述的相同问题
    • @MouaadAbdelghafourAITALI 你找到解决方案了吗?
    【解决方案2】:

    除了允许使用上述权限行访问互联网(或获取主机名查找错误)以在 RELEASE 模式下从应用程序使用 http.get 之外,您还需要在主应用程序部分中添加以下更多行清单文件,否则它只是静默挂起(超时,如果你有的话)。

    我无法理解为什么这些限制在调试时不适用。

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
       <data android:host="YOUR.DOMAIN.NAME"
                        android:scheme="https" />
    </intent-filter>
    

    我不确定这是否可以进一步修剪。

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 2019-07-12
      • 2019-11-15
      • 2011-07-25
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多