Using an api and http request in Flutter
925
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
Author by
vinay
Updated on January 03, 2023Comments
-
vinay over 1 year
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?
-
OMi Shah about 2 years
var = response = await http.get(Uri.parse(url));
should work. -
Ameer Hamza about 2 yearsvar = response = await http.get(Uri.parse(uri.parse(url)));
-
Timotej Leginus about 2 yearsFor future reference, be sure to have a .fromJson() constructor and a .toJson() functions for your JSON objects represented in Dart.
-
-
vinay about 2 yearscan i get help for the secound error @Ravindra S. Patil please
-
Ravindra S. Patil about 2 years@vinaym, yes please tell
-
OMi Shah about 2 yearsPlease refrain from posting duplicate answers, instead mark the question as duplicate ;)