'Flutter When i fetch data from api showing SocketException: Failed host lookup: (OS Error: No address associated with hostname, errno = 7)

eg: details about the questions .........................................................................................

When i fetch data from api showing SocketException: Failed host lookup: (OS Error: No address associated with hostname, errno = 7).not able to load and display the data in the list form.

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';


class SecondScreen extends StatelessWidget {
  final String apiUrl = "https://www.sofikart.com/MobileApi/banners";

  Future<List<dynamic>> fetchUsers() async {

    var result = await http.get(apiUrl, headers: {HttpHeaders.authorizationHeader: 'SOFIKART-*2021#',},);

    return json.decode(result.body)['data'];
  }


  String id(dynamic user) {
    return user['id'];
  }

  String image(dynamic user) {
    return user['image'];
  }

  String cat_id(dynamic user) {
    return user['cat_id'];
  }

  String product_id(dynamic user) {
    return user['product_id'];
  }

  String url(dynamic user) {
    return user['url'];
  }

  String status(dynamic user) {
    return user['status'];
  }

  String ordering(dynamic user) {
    return user['ordering'];
  }

  String updated(dynamic user) {
    return user['updated'];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ଆଜିର ରାଶିଫଳ'),
        centerTitle: true,
      ),
      body: Container(
        child: FutureBuilder<List<dynamic>>(
          future: fetchUsers(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              print(id(snapshot.data[0]));
              return ListView.builder(
                  padding: EdgeInsets.all(8),
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Card(
                      child: Column(
                        children: <Widget>[
                          ListTile(
                            leading: CircleAvatar(
                                radius: 30,
                                backgroundImage: NetworkImage(
                                    snapshot.data[index]['image'])),
                            title: Text(product_id(snapshot.data[index])),
                            subtitle: Text(status(snapshot.data[index])),
                            trailing: Text(ordering(snapshot.data[index])),
                          )
                        ],
                      ),
                    );
                  });
            } else {
              return Center(child: CircularProgressIndicator());
            }
          },
        ),
      ),
    );
  }
}


Solution 1:[1]

Please add permission for internet in manifest file :

path : android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

Solution 2:[2]

Hi you also have what do you display your images an image network I guess?? If this is the case it is normal that your images do not work because this widget needs a connection to work you must turn to the cache_network_image it is a package that will keep local images from links

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hardik Mehta
Solution 2 Mamadou Daye DIAKITE