0

When I fetch data from an API it is 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());
            }
          },
        ),
      ),
    );
  }
}
4
  • The internet is working on testing device? Commented Feb 4, 2022 at 6:45
  • Yes working on testing devices Commented Feb 4, 2022 at 7:11
  • Your app has internet permissions in Android Manifest? Commented Feb 4, 2022 at 7:22
  • yes i had internet permission but not working ..... Commented Feb 4, 2022 at 7:26

2 Answers 2

3

Please add permission for internet in manifest file :

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

<uses-permission android:name="android.permission.INTERNET"/>
Sign up to request clarification or add additional context in comments.

11 Comments

already added but now working showing SocketException: Failed host lookup: (OS Error: No address associated with hostname, errno = 7)
can you try this : await http.get(Uri.parse(apiUrl), headers: {HttpHeaders.authorizationHeader: 'SOFIKART-*2021#',},); instead of await http.get(apiUrl, headers: {HttpHeaders.authorizationHeader: 'SOFIKART-*2021#',},);
No showing same error .....
Seems to be cors related issue : cors-test.codehappy.dev/… you need to ask Access-Control-Allow-Origin at api side or enable cors at apis side
Thank you this is ths issues of Access-Control-Allow-Origin in this api . i have tried other api working fine.
|
0

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.