Android Flutter and http get

Issue

I’m figuring out how to get status code 200 from https://icanhazip.com
This code only works on desktop and android emulator, but it doesn’t work on the android mobile. The variable string id178 is always blank . It doesn’t even print ‘TEST’
setState() should update the variable when you tap
Thank you

This is the manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.test">
   <!-- The INTERNET permission is required for development. Specifically,
     the Flutter tool needs it to communicate with the running application
     to allow setting breakpoints, to provide hot reload, etc.
   -->
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>



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

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

class App extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
   return MaterialApp(
      home: HomePage(),
   );
 } 
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}


class _HomePageState extends State<HomePage> {
  String id178 = '';

  getMachinesId() async {
    var url = Uri.parse('https://icanhazip.com');
    var res = await http.get(url);
    id178 = '${res.statusCode} TEST ';
  }

  @override
  void initState() {
    super.initState();
    getMachinesId();
  }

 @override
 Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black87,
        title: const Text("TEST !",
        style: TextStyle(
            fontSize: 20,
            fontStyle: FontStyle.italic,
            fontWeight: FontWeight.bold,
            color: Color(0xFFbf9b0a))),
      centerTitle: true,
      ),
      body: Center(
        child: Text(
          "Server Machine: $id178\n",
          style: const TextStyle(
            fontSize: 10,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),

      floatingActionButton: FloatingActionButton(
         onPressed: () {
           setState(() {
             getMachinesId();
           });
         },
         child: const Icon(Icons.verified_sharp),
      ),
  );
 } 
}

Solution

As far as I know there are three manifest file

src/debug
src/main (release)
src/profile

I’ve edited the wrong manifest…

Edit:

After you have created new project in visual code, there is a manifest for each subfolder :

android/app/src/main

android/app/src/debug

android/app/src/profile

In terminal window I run :

flutter build apk --release

so the right "AndroidManifest.xml" file is in the "main" folder

Answered By – ghost239

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published