Flutter - Android - http.get在发布模式下永远不会返回

7

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

我对Flutter很陌生,无法解决这个问题。在github上有一个类似的问题,但与Web Flutter有关。

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

  • 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
2个回答

4

您需要将网络权限添加到AndroidManifest.xml中,在Flutter Networking的官方文档中可以查看此内容。 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>

1
我已经添加了INTERNET权限,但问题仍然存在,我遇到了与问题描述中相同的问题。 - Mouaad Abdelghafour AITALI
@MouaadAbdelghafourAITALI 你找到解决方案了吗? - Raphaël

0
除了允许通过上述权限行访问互联网(或出现主机名查找错误)之外,要在发布模式下从应用程序中使用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>

我不确定这是否可以进一步简化。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接