1
    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

How can I solve this?

3
  • 1
    var = response = await http.get(Uri.parse(url)); should work. Commented Feb 23, 2022 at 11:16
  • var = response = await http.get(Uri.parse(uri.parse(url))); Commented Feb 23, 2022 at 11:19
  • For future reference, be sure to have a .fromJson() constructor and a .toJson() functions for your JSON objects represented in Dart. Commented Feb 23, 2022 at 12:00

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

can i get help for the secound error @Ravindra S. Patil please
@vinaym, yes please tell
Please refrain from posting duplicate answers, instead mark the question as duplicate ;)
0

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);
              }
            };
          }
        }

Comments

0

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)); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.