我正在为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