0

I am trying to get the IP Address to show but I am getting null. Also this works when I simulate it, but not when I am trying it on an android device. What could be the issue here? Any help would be appreciated! Here is my code:

[import 'package:bottom_navigation_test/sidemenu.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as https;
import 'widget/info_widget.dart';

class MyIP extends StatefulWidget {
  @override
  _MyIP createState() => _MyIP();
}

class _MyIP extends State<MyIP> {
  Map<String, dynamic> map = {};

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

  Future init() async {
    final ipAddress = await MyIPInfo.getIPAddress();

    if (!mounted) return;
    setState(() => map = {
          'IP Address:': ipAddress,
        });
  }

  Widget build(BuildContext context) => Scaffold(
        drawer: SideMenu(),
        appBar: AppBar(
          backgroundColor: const Color.fromARGB(255, 89, 137, 199),
          title: const Text('My IP'),
        ),
        body: InfoWidget(map: map),
      );
}

class MyIPInfo {
  static Future<String?> getIPAddress() async {
    try {
      final url = Uri.parse('https://api.ipify.org');
      final response = await https.get(url);

      return response.statusCode == 200 ? response.body : null;
    } catch (e) {
      return null;
    }
  }
}][1]
Panos_42
  • 9
  • 1
  • 5

0 Answers0