Dart Is there a way to get HTTPS content? - flutter

I really need help. I try to get the rss feed from this url:
- https://www.france24.com/fr/rss?time=1573661731673
But I don't know how to do it when using a https url, I tried to use the HTTP dart library. Here is my code:
import 'package:html/parser.dart';
import 'package:html/dom.dart';
import 'dart:async';
import 'package:http/http.dart';
class Parser {
final String urlSite = "http://www.france24.com/fr/actualites/rss";
Future chargerRSS() async{
final reponse = await get(urlSite);
if(reponse.statusCode == 200){
final feed = RssFeed.parse(reponse.body);
print("$feed at parser");
}
else{
print("Erreur: ${reponse.statusCode}");
}
}
}```

Related

The argument type 'UserModel' can't be assigned to the parameter type 'Iterable<UserData>'

this is mycode .please solve this problem
import 'dart:convert' as convert;
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:internetconnectivityusingbloc/repos/user_model.dart';
class UserRepositories {
Future<void> getUsers() async {
List<UserData>? userData = [];
String urlEndPoint = "https://reqres.in/api/users?page=2";
// Await the http get response, then decode the json-formatted response.
Response response = await http.get(Uri.parse(urlEndPoint));
if (response.statusCode == 200) {
userData = [];
// final List result = jsonDecode(response.body);
return userData.addAll(UserModel.fromjson(response.body));
} else {
throw Exception(response.reasonPhrase);
// print('Request failed with status: ${response.statusCode}.');
}
}
}
i am trying to solve this problem .but i am failed.so i expecting u are solve this problem
Add [] inside addAll:
return userData.addAll([UserModel.fromjson(response.body)]);
If you are only adding one model is better to only use the method add.
Howerver you are trying to add a type UserModel to a list of UserData and that might throw another error if UserModel is not a child of UserData. So if you are expecting to fill the variable userData you should use UserData.fromjson(response.body) to fill the new data, so you should adjust UserData parameters to get the data that response.body will bring.

Using http.dart to Call CoinMarketCap API. Not sure what to do

So I'm new to flutter and dart and am trying to call from the CoinMarketCap API. I'm using the HTTP package to call the data and the API. I'm not super familiar with them but here's what I came up with...
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<Payload> getCryptoPrices() async {
var response = await http.get(Uri.parse(
"https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=5000&convert=USD"),
headers: {
'X-CMC_PRO_API_KEY': 'my-key',
"Accept": "application/json",
});
if (response.statusCode == 200) {
Payload payload = payloadFromJson(data.body);
return payload;
}
}
I get a couple of errors:
The name 'Payload' isn't a type so it can't be used as a type argument
The function 'payloadFromJson' isn't defined
Undefined name 'data'
Am I not successfully importing JSON? I'm not sure how to fix the error. What do I need to do to successfully make a API Call? Any feedback would be great.
CODE UPDATED #1
import 'package:wnetworking/wnetworking.dart';
class CoinMarketCap {
static const _apiKey = '111111111111111111111111111111';
static const _url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency';
static Future<void> getListingLatest(int limit) async {
var url = '$_url/listings/latest?start=1&limit=$limit&convert=USD';
var result = await HttpReqService.get<JMap>(
url,
auth: AuthType.apiKey,
authData: MapEntry('X-CMC_PRO_API_KEY', _apiKey)
);
var coins = (result?['data'] as List).cast<JMap>().map<String>((e) => e['name']);
print(coins);
}
}
void main(List<String> args) async {
await CoinMarketCap.getListingLatest(7);
print('\nJob done!');
}
Output:
(Bitcoin, Ethereum, Tether, USD Coin, BNB, XRP, Cardano)
Job done!
'Payload' is not a flutter class so it does not exist. were you trying to use a Custom made Class?
the 'payloadFromJson' error means it does not exist so you probably did not import it properly if it is in another class
Undefined name 'data' means that data has not been defined if you want the body of the response use 'response.body'

how to add http caching using "dio_http_cache" package to flutter

I have a code that I use to download data, through a query (get) and display it. But I want to change something, I want to cache the data and display it. So that if the device was offline, I could display the latest data. Someone is already doing this, I will be grateful for your help)
import 'package:dio/dio.dart';
import 'package:dio_http_cache/dio_http_cache.dart';
import 'package:test_axles/models/seals_model.dart';
import 'package:test_axles/setting_var/globalvar.dart' as global;
class ApiProvider {
final Dio _dio = Dio();
final String _url = global.urlVar;
Future<Objects> fetchSealsList() async {
try {
Response response = await _dio.get(_url);
return Objects.fromJson(response.data);
} catch (error, stacktrace) {
return Objects.withError(" ");
}
}
}

This function has a return type of 'FutureOr<Xmodel>, but doesnt end with a return statement

I'm trying to integrate a stock API into my app. I'm trying to create a service page but I get this error. I searched for similar topics but I couldn't find a solution.
import 'dart:convert';
import 'dart:io';
import 'package:myapp/models/apis/stocks.dart';
import 'package:http/http.dart' as http;
class StocksService {
final String url = "https://api.collectapi.com/economy/hisseSenedi";
Future<StocksModel> fetchStocks() async {
var res = await http.get(Uri.parse(url),
headers: {
HttpHeaders.authorizationHeader:
"apikey xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
);
if (res.statusCode == 200) {
var jsonBody = StocksModel.fromJson(jsonDecode(res.body));
return jsonBody;
} else {
print("İstek başarısız oldu => ${res.statusCode}");
}
}
}
You are not returning anything in your else block. It this is what you require in your code then change the return type of the function to dynamic. And if not then you are required to return the same data type in the else block too.

Rx Flutter Request List From JsonPalceHolder

I try to get List from jsonPlaceHolder using flutter rxdart stream and try to apply bloc pattern on it.
this class that response for get post response from api
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/post_item.dart';
class ItemApi {
Future<List<JsonItem>> getPost() async {
String _url = 'https://jsonplaceholder.typicode.com/posts';
final _response = await http.get(_url);
if (_response.statusCode == 200) {
return (json.decode(_response.body) as List)
.map((jsonItem) => JsonItem.fromJson(jsonItem))
.toList();
}
}
}
I using repository class to wrap using ItemApi class
import 'json_item_request.dart';
import '../models/post_item.dart';
class Repository{
final jsonItemResponse = ItemApi();
Future<List<JsonItem>> getItem() => jsonItemResponse.getPost();
}
at the last i using bloc class that response for get data and set it inside PublishSubject
import '../models/post_item.dart';
import '../resouces/repository.dart';
import 'package:rxdart/rxdart.dart';
class JsonBloc {
final _repository = Repository();
final _streamOfJsonList = PublishSubject<List<JsonItem>>();
Observable<List<JsonItem>> get jsonList=> _streamOfJsonList.stream;
fetchAllPost() async{
Future<List<JsonItem>> list = _repository.getItem();
}
dispose(){
_streamOfJsonList.close();
}
}
My question is how i can set response inside _streamOfJsonList variable to using it when list changed.
Sounds like you already have all the moving parts connected? If so you just need to add the item list to the PublishSubject:
void fetchAllPost() async {
List<JsonItem> list = await _repository.getItem();
_streamOfJsonList.add(list);
}
This will trigger the onListen callback with the new list on anything that is listening to the stream.
You can add error and data to ReplaySubject like below :
void fetchAllPost() async {
List<JsonItem> list = await _repository.getItem();
if (list != null) {
_streamOfJsonList.sink.add(list);
} else {
_streamOfJsonList.addError("ERROR");
}
}