Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can any one help me in this api and http request in flutter


    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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading