7

I am writing a flutter app for android and ios. To log in, the app uses the http package (version 0.12.2) : http.Response response = await http.get(url);. In iOS, everything works fine. In Android, the debug mode works also fine, however, if I build the apk and install the app from this release apk, the async method http.get never returns. I use Android 9 on a OnePlus 3 (OxygenOS 9.0.6).

I am very new to flutter and can't figure this out. A similar issue is open in github but concerns flutter for web.

Here is a minimal code to reproduce. To test it, you should build the apk (Build > Flutter > Build apk)), copy and paste the apk in your phone files, install the app from the apk, press the button PRESS ME, done will never be displayed.

  • 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 Answers 2

4

Hi you need to add networking permission to AndroidManifest.xml check this official documentation on flutter networking https://flutter.dev/docs/development/data-and-backend/networking

it is located in android/src/main/AndroidManifest.xml

<manifest xmlns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <application ...
</manifest>
Sign up to request clarification or add additional context in comments.

2 Comments

I've already added the INTERNET permission, but the issue still present, I've the same problem described in the question
@MouaadAbdelghafourAITALI were you able to find a solution ?
0

Apart from allowing access to the internet with the abovementioned permission line, (or get a hostname lookup error) to use http.get from an app in RELEASE mode you need to add the following more lines within the application section of your main manifest file else it just silently hangs (times out, if you have one).

It is beyond me why these restrictions do not apply when debugging.

<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>

I am not sure if this can be trimmed further.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.