Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I fetch data from async function in flutter?

I’m creating a simple app that uses an api which returns my adress ip i’m trying to fetch my ip adress in the app but I got this error "type ‘null’ is not a subtype of type ‘map String dynamic ‘ in type cast"
can you help me please

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
void main() => runApp(MaterialApp(
    home: IP(),
    ));

class IP extends StatefulWidget {
  const IP({Key? key}) : super(key: key);

  @override
  State<IP> createState() => _IPState();
}

class _IPState extends State<IP> {

  Map data = {};

void getIp() async {
  Response response = await get(
  Uri.parse('https://api.ipify.org/?format=json'));
  Map data = jsonDecode(response.body);
  String ip = data['ip'];
  print(ip);
}

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

  @override
  Widget build(BuildContext context) {
    data = data.isNotEmpty ? data : ModalRoute.of(context)!.settings.arguments as Map<String, dynamic>;
    return  Scaffold(
      appBar: AppBar(
        title: Text('My IP adress is'),
        centerTitle: true,
        elevation: 0.0,
        backgroundColor: Colors.cyan,
      ),
      body: Center(
         child: Text('$data[ip]')
      )
    );
  }
}



>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

try this:

class _IPState extends State<IP> {
  

  Future<String> getIp() async {
    Response response =
        await get(Uri.parse('https://api.ipify.org/?format=json'));
    Map data = jsonDecode(response.body);
    return data['ip'];
  }

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

  @override
  Widget build(BuildContext context) {
   
    return Scaffold(
      appBar: AppBar(
        title: Text('My IP adress is'),
        centerTitle: true,
        elevation: 0.0,
        backgroundColor: Colors.cyan,
      ),
      body: FutureBuilder(
        future: getIp(),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Text('Loading....');
            default:
              if (snapshot.hasError)
                return Text('Error: ${snapshot.error}');
              else
                return Center(child: Text('${snapshot.data}'));
          }
        },
      ),
    );
  }
}

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading