Home > Back-end >  can any one help me in this api and http request in flutter
can any one help me in this api and http request in flutter

Time:02-24


    import 'dart:convert';
    import 'package:http/http.dart' as http;
    import 'package:newsapi_project/artical.dart';
    
    
    class News{ 
      List<ArticalModel> news =[];
      Future<void> getNews()async{
       String url="https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=8efc3612216c44ccba7a8251dfbed587";
       var response=await http.get( url); //here its throwing the error//
       var jsonData= jsonDecode(response.body);
       if (jsonData["status"]=="ok"){
         jsonData["articles"].forEach(element){ //=> and even to in .forEach//
           if(element["urlToImagel"] != null && element["description"] != null ){
    
             ArticalModel articalmodel = ArticalModel(
               title:element["title"],
               author:element["author"],
               description:element["description"],
               url:element["url"],
              urlToImage:element["urlToImage"],
              content:element["content"]
             );
             news.add(articalmodel);
           }
         };
       }
      }
    
    }

The argument type 'String' can't be assigned to the parameter type 'Uri'.dart

   var response=await http.get( url); // this is where i am getting error

Function expressions can't be named Try removing the name, or moving the function expression to a function declaration statement. .forEach this is where i am getting error

Expected an identifier. .forEach this is where i am getting error

any way to solve this please help me to get rid of this

CodePudding user response:

Try below code hope its help to you. Change your API url declaration.

String url = 'https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=8efc3612216c44ccba7a8251dfbed587';

var response=await http.get(Uri.parse(url));

Refer documenation here

CodePudding user response:

Try This! Code Snippet

 Future<void> getNews()async{
          String url="https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=8efc3612216c44ccba7a8251dfbed587";
          var response=await http.get(Uri.parse(url)); //here its throwing the error//
          var jsonData= jsonDecode(response.body);
          if (**jsonData.statusCode==200**){
            jsonData["articles"].forEach(element){ //=> and even to in .forEach//
              if(element["urlToImagel"] != null && element["description"] != null ){
    
                ArticalModel articalmodel = ArticalModel(
                    title:element["title"],
                    author:element["author"],
                    description:element["description"],
                    url:element["url"],
                    urlToImage:element["urlToImage"],
                    content:element["content"]
                );
                news.add(articalmodel);
              }
            };
          }
        }

CodePudding user response:

get method definition

Future<Response> get(
 Uri url,
  {Map<String, String>? headers}
  )

and must parse url like this

 String url="https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=8efc3612216c44ccba7a8251dfbed587";

var response=await http.get( Uri.parse(url)); 
  • Related