Home > Software engineering >  Instance of 'Future<String>'
Instance of 'Future<String>'

Time:07-22

In this application, I used a package called get_ip_address and I want to get the ip address data and print it with this package.

 static Future<String> getIp() async {
try {
  /// Initialize Ip Address
  var ipAddressType = IpAddress(type: RequestType.json);

  /// Get the IpAddress based on requestType.
  dynamic ipAddress = await ipAddressType.getIpAddress();
  return ipAddress.toString();
} on IpAddressException catch (exception) {
  /// Handle the exception.-
  return exception.message;
}

}

But when I use it, I get the output of Instance of 'Future'. Is there anyone who can help?

ElevatedButton(
                style: ButtonStyle(
                    backgroundColor: isClick == true
                        ? MaterialStateProperty.all(Colors.green.shade800)
                        : MaterialStateProperty.all(Colors.grey.shade400),
                    textStyle: MaterialStateProperty.all<TextStyle>(
                      const TextStyle(
                        fontSize: 18,
                        fontWeight: FontWeight.bold,
                      ),
                    )),
                onPressed: isClick
                    ? () async {
                        isClick = false;
                        final DateTime dateCreated = DateTime.now();
                        setState(() {
                          dateCreated;
                        });
                        final user = User(
                            name: _nameController.text,
                            surname: _surnameController.text,
                            email: _emailController.text,
                            year: _selectedValue,
                            dateCreated: DateFormat('dd/MM/y - HH:mm:ss').format(dateCreated),
                            ipAddress: ip1adress);
                        hasInternet = await InternetConnectionChecker().hasConnection;
                        await UserService.insert([user.toJson()]);
                        if (hasInternet) {
                          showToast(StringConstants.registerOkayText, duration: 2, gravity: Toast.bottom);
                        } else {
                          showToast(StringConstants.registerNotOkayText, duration: 5, gravity: Toast.bottom);
                        }
                      }
                    : null,
                child: Text(
                  StringConstants.saveButtonText,
                ),
              ),

CodePudding user response:

change this part :

final ip = await getIp();

final user = User(
    name: _nameController.text,
    surname: _surnameController.text,
    email: _emailController.text,
    year: _selectedValue,
    dateCreated: DateFormat('dd/MM/y - HH:mm:ss').format(dateCreated),
    ipAddress: ip);

CodePudding user response:

   String __ipAddress=""; 
    static Future<String> getIp() async {
    try {
      /// Initialize Ip Address
      var ipAddressType = IpAddress(type: RequestType.json);
    
      /// Get the IpAddress based on requestType.
      dynamic ipAddress = await ipAddressType.getIpAddress();
     setState(() => _ipAddress = ipAddress);
      
    } on IpAddressException catch (exception) {
      /// Handle the exception.-
      return exception.message;
    }
    }

try this.

CodePudding user response:

Try this

    
     const Future<String> getIp() async {
    try {
      /// Initialize Ip Address
      var ipAddressType = IpAddress(type: RequestType.json);
      /// Get the IpAddress based on requestType.
      int ipAddress = await ipAddressType.getIpAddress();
      return ipAddress.toString();
    } on IpAddressException catch (e) {
      /// Handle the exception.-
      return e.message;
    }

    
    final userIp = await getIp();
    
    final newUser = User(
        name: nameCtr.text,
        year: setValue,
        surname: surnameCtr.text,
        email: emailCtr.text,
        dateCreated: DateFormat('dd/MM/y - HH:mm:ss').format(dateCreated),
        ipAddress: userIp);
  • Related