0

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());
            }
          },
        ),
      ),
    );
  }
}
Debendra
  • 35
  • 2
  • 13

2 Answers2

3

Please add permission for internet in manifest file :

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

<uses-permission android:name="android.permission.INTERNET"/>
Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14
  • already added but now working showing SocketException: Failed host lookup: (OS Error: No address associated with hostname, errno = 7) – Debendra Feb 04 '22 at 07:27
  • 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#',},); – Hardik Mehta Feb 04 '22 at 08:33
  • No showing same error ..... – Debendra Feb 04 '22 at 08:58
  • Seems to be cors related issue : https://cors-test.codehappy.dev/?url=https%3A%2F%2Fwww.sofikart.com%2FMobileApi%2Fbanners&method=get you need to ask Access-Control-Allow-Origin at api side or enable cors at apis side – Hardik Mehta Feb 04 '22 at 09:14
  • Thank you above problem fixed but Now showing StatusCode 403 ForBidden. – Debendra Feb 04 '22 at 09:15
  • Hardik Mehta How to fixed the StatusCode 403 ForBidden error. Can you help me ? – Debendra Feb 04 '22 at 09:18
  • @Debendra : seems to be authentication error. is your headers are correct ? – Hardik Mehta Feb 04 '22 at 09:23
  • 1
    Thank you this is ths issues of Access-Control-Allow-Origin in this api . i have tried other api working fine. – Debendra Feb 04 '22 at 09:32
  • @ Hardik Mehta Can you tell me how to display fetched image from api in gridview – Debendra Feb 04 '22 at 09:53
  • you can follow this might help you : https://stackoverflow.com/questions/52483425/how-to-create-a-gridview-within-a-listview-by-flutter-with-json-api?rq=1 – Hardik Mehta Feb 04 '22 at 11:43
  • Ok i'will check. – Debendra Feb 04 '22 at 11:58
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

  • 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](/help/how-to-answer). – Dani3le_ Apr 21 '22 at 08:20