flutter - android - http.get never returns in release mode

791

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>
Share:
791
Raphaël
Author by

Raphaël

Updated on December 18, 2022

Comments

  • Raphaël
    Raphaël over 1 year

    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