1

Pls i have been trying to make a post and a get request to a server but i have been getting error. it was working initially but i tried running the code again and it keeps on showing errors, i have tried using different url but i have been getting the same error. below is the error am getting..

E/flutter ( 5388): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'swapi.dev'
E/flutter ( 5388): #0      IOClient.send
package:http/src/io_client.dart:88
E/flutter ( 5388): <asynchronous suspension>
E/flutter ( 5388): #1      BaseClient._sendUnstreamed
package:http/src/base_client.dart:93
E/flutter ( 5388): <asynchronous suspension>
E/flutter ( 5388): #2      _withClient
package:http/http.dart:164
E/flutter ( 5388): <asynchronous suspension>

This is the function am using to send the get request

import 'package:http/http.dart' as http;

class _LinkedBankAccountsState extends State<LinkedBankAccounts> {
  
  Future<void>  fetchData() async{
    var url = Uri.parse('https://swapi.dev/api/people');
    var response = await http.get(url);
    print(response);
  
  }

this is the widget that calls the function onclick

GestureDetector(
    onTap: fetchData,
    child: BankWidget(
         desc: 'details',
         title: 'people',
         imageUrl:'lib/assets/images/flipLogo.png',
         ),
  ),

below is the full widget

// ignore_for_file: prefer_const_constructors

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../widgets/background-image.dart';
import '../widgets/actions.dart';
import '../widgets/linked_bank_item.dart';
import '../widgets/linked_bank_item.dart';

class LinkedBankAccounts extends StatefulWidget {
  const LinkedBankAccounts({super.key});

  @override
  State<LinkedBankAccounts> createState() => _LinkedBankAccountsState();
}

class _LinkedBankAccountsState extends State<LinkedBankAccounts> {
  
  Future<void>  fetchData() async{
    var url = Uri.parse('https://swapi.dev/api/people');
    var response = await http.get(url);
    print(response);
  
  }

  @override
  Widget build(BuildContext context) {
    const routeName = '/linked-banks';
    return Stack(
      children: [
        backgroundImage(),
        Scaffold(
          backgroundColor: Colors.transparent,
                 appBar: AppBar(
            backgroundColor: Colors.transparent,
            elevation: 0,
            leading: IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
              ),
            ),
           
           
          ),
          body: SingleChildScrollView(
            child: SafeArea(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                Container(
                  padding: EdgeInsets.only(top: 20,left: 30,right: 35) ,
                  height: 200,
                  width: 400 ,
                  decoration: BoxDecoration(
                    color:Colors.grey[600]!.withOpacity(0.3),
                    borderRadius: BorderRadius.circular(16), 
                    boxShadow: [
                      BoxShadow(
                        color: Colors.white.withOpacity(0.1),
                        spreadRadius: 1,
                        blurRadius: 7,
                        offset: Offset(0, 3)
                      )
                    ]
                    ), 
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      
                      children: [
                        
                        Container(
                          width: 50,
                          height: 50,
                          decoration: BoxDecoration(
                            color: Color(0XFF00B686),
                            boxShadow: [
                            BoxShadow(
                              color: Colors.black.withOpacity(0.1),
                              blurRadius: 8,
                              spreadRadius: 3,
                            )
                            ],
                            border: Border.all(
                              width: 1.5,
                              color: Colors.white,
                            ),
                            borderRadius: BorderRadius.circular(40)
        
                          ),
                
                          child: CircleAvatar(
                              backgroundImage: AssetImage('lib/assets/images/avatar.png') ,
                          ),
                        ),
                        
                        Column(
                         
                          children: [
                            const Text(
                              'STANLEY OMEJE',
                              style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontSize: 18
                              ),
                            ),
                            SizedBox(height: 10,),
                            Row(
                              // ignore: prefer_const_literals_to_create_immutables
                              children: [
                                Icon(Icons.keyboard_double_arrow_right_outlined,color: Colors.white, size: 30,),
                                SizedBox(width: 10,),
                                Text(
                                  'stanzealot',
                                   style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 18
                                    ),
                                ),
                                SizedBox(width: 10,),
                                Icon(
                                  Icons.copy_outlined,
                                  color: Colors.white,
                                  
                                )
                              ],
                            ),

                            SizedBox(height: 10,),
                            Text(
                              "\N0.00",
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 18,
                                fontWeight: FontWeight.bold
                              ),
                              )
                          ],
                        )

                      ],
                    ),
                ),

                SizedBox(height:30),
              
                 Column(
                   children: [
                     SizedBox(
                      height: 400,
                       child: ListView(
                          scrollDirection: Axis.vertical,
                          
                          children: [
                           
                          
                        GestureDetector(
                          onTap: fetchData,
                          child: BankWidget(
                            desc: 'details',
                            title: 'people',
                            imageUrl:'lib/assets/images/flipLogo.png',
                          ),
                        ),
                
                ]
                ),
                     ),
                   ],
                 ),
              

                //

              ],) 
              ),
          ) ,
        )
      ],
    );
  }
}


Thanks in Advance

1 Answers1

0

Try this one

 Future<void> fetchData() async {
  try {
    final res = await http.get(Uri.parse('https://swapi.dev/api/people'));
    print(res.statusCode);
  } catch (e) {
    print(e);
  }
}