2

How to get user IP or just country in Flutter (Dart)? I can`t find.

And how import this library? import 'package:location/location.dart';

5
  • 1
    What do you mean with "how to import this library"? The line is importing the library. Perhaps you missed to add it to dependencies in pubspec.yaml - see pub.dartlang.org/packages/location#-installing-tab- Commented Jul 3, 2018 at 7:24
  • 1
    Yes, that's what I meant. Because when I wrote this import, system pointed out an error. Thanks for the help. I'm working with Fletter for only a week, so there are a lot of simple things that I don't know or don't understand. Commented Jul 3, 2018 at 7:42
  • Don't worry too much about the downvotes. You'll figure out how to ask good questions that get upvotes after a while. Please check stackoverflow.com/help for more details about what is considered a good question on SO. Commented Jul 3, 2018 at 7:45
  • Everthing you need to get started is here Commented Jul 3, 2018 at 11:24
  • Thank you. But I have not found how to get the user IP or the name of his country( Commented Jul 3, 2018 at 11:39

1 Answer 1

1

To get IP address, you can use the dart:io library. Beware, doesn't look like Android is supported at time of answering this.

import 'package:flutter/material.dart';
import 'dart:io';

class GetIPScreen extends StatefulWidget {
  @override
  _GetIPScreenState createState() {
    return new _GetIPScreenState();
  }
}

class _GetIPScreenState extends State<GetIPScreen> {
  String _ipAddress;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Padding(
              padding: const EdgeInsets.symmetric(vertical: 16.0),
              child: new Text("IP Address: $_ipAddress"),
            ),
            new RaisedButton(
                child: new Text("Get IP"),
                onPressed: () async {
                  debugPrint("pressed button");
                  if (NetworkInterface.listSupported) {
                    List<NetworkInterface> networkInterfaces = await NetworkInterface.list(
                      type: InternetAddressType.IP_V4
                    );
                    networkInterfaces.forEach((i) => debugPrint("name: ${i.name}, address: ${i.addresses}"));
                    setState(() {
                      _ipAddress = networkInterfaces[0].addresses.first.address;
                    });
                  } else {
                    setState(() {
                      _ipAddress = "Not Supported";
                    });
                  }
                })
          ],
        ),
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

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.