Home > Net >  Android Flutter and http get
Android Flutter and http get

Time:08-28

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),
      ),
  );
 } 
}

CodePudding user response:

You missed print and set state

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

CodePudding user response:

As far as I know there are three manifest file

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

I've edited the wrong manifest...

  • Related