So I have made a News App using APIs available on the internet. So I had to use different API for each different Category. There are more than 9 categories, So this is making my app load very slow. So, what is the solution to this. How can I only call 2 APIs at the initial state and rest of the others after some time when the app has loaded.
See the code below:
class NewsCard extends StatefulWidget {
#override
_NewsCardState createState() => _NewsCardState();
}
class _NewsCardState extends State<NewsCard>
with SingleTickerProviderStateMixin {
TabController tabController;
static DateTime now = DateTime.now();
String formattedDate;
List newsData;
List topNews1;
List businessNews1;
List worldNews1;
List sportsNews1;
List entertainmentNews1;
List educationNews1;
List tvnews1;
List electionNews1;
List lifeNews1;
bool isLoading = true;
final String toi= "https://timesofindia.indiatimes.com/";
final String topNews =
"API url";
final String sportsNews =
"API url";
final String businessNews =
"API url";
final String worldNews =
"API url ";
final String entertainmentNews =
"API url ";
final String educationNews =
"API url";
final String tvNews =
"API url ";
final String electionNews =
"API url";
final String lifeNews =
"API url";
final String url =
"https://newsapi.org/v2/top-headlines?sources=google-news-in&apiKey=";
Future getData() async {
var response = await http.get(
Uri.encodeFull(url),
headers: {
HttpHeaders.authorizationHeader: ""
},
);
var response1 = await http.get(
Uri.encodeFull(topNews),
);
var response2 = await http.get(
Uri.encodeFull(businessNews),
);
var response3 = await http.get(
Uri.encodeFull(worldNews),
);
var response4 = await http.get(
Uri.encodeFull(sportsNews),
);
var response5 = await http.get(
Uri.encodeFull(entertainmentNews),
);
var response6 = await http.get(
Uri.encodeFull(tvNews),
);
var response7 = await http.get(
Uri.encodeFull(lifeNews),
);
var response8 = await http.get(
Uri.encodeFull(electionNews),
);
var response9 = await http.get(
Uri.encodeFull(educationNews),
);
List data = jsonDecode(response.body)['articles'];
List topNewsData = jsonDecode(response1.body)['stories'];
List businessNewsData = jsonDecode(response2.body)['items'][0]['stories'];
List worldNewsData = jsonDecode(response3.body)['items'][0]['stories'];
List sportsNewsData = jsonDecode(response4.body)['items'][0]['stories'];
List entertainmentNewsData = jsonDecode(response5.body)['items'][0]['stories'];
List tvNewsData = jsonDecode(response6.body)['items'][0]['stories'];
List lifeNewsData = jsonDecode(response7.body)['items'][0]['stories'];
List electionsNewsData = jsonDecode(response8.body)['items'][0]['stories'];
List educationNewsData = jsonDecode(response9.body)['items'][0]['stories'];
setState(() {
newsData = data;
topNews1 = topNewsData;
businessNews1 = businessNewsData;
worldNews1 = worldNewsData;
sportsNews1 = sportsNewsData;
entertainmentNews1 = entertainmentNewsData;
tvnews1 = tvNewsData;
lifeNews1=lifeNewsData;
electionNews1 = electionsNewsData;
educationNews1 = educationNewsData;
isLoading = false; //this is for the initial loading, this is taking too much of time.
});
}
#override
void initState() {
super.initState();
this.getData();
tabController = TabController(vsync: this, length: 9);
}
Inside the Scaffold
isLoading
?Column(....):Column(.....)
Do keep in Mind that i am a beginner in Flutter, So if my method of approach is wrong then kindly request me to do the perfect approach.
Since there is no dependency between different api calls (api call 1 need not wait for api call 0 to finish), better to start them all and await for the results at end. So don't use await for every api call. Instead use Future.wait to wait for all futures at the end. Something like:
Future getData() async {
List<Future> responseFutures = [
http.get(
Uri.encodeFull(url),
headers: {HttpHeaders.authorizationHeader: ""},
),
http.get(
Uri.encodeFull(topNews),
),
http.get(
Uri.encodeFull(businessNews),
),
http.get(
Uri.encodeFull(worldNews),
),
http.get(
Uri.encodeFull(sportsNews),
),
http.get(
Uri.encodeFull(entertainmentNews),
),
http.get(
Uri.encodeFull(tvNews),
),
http.get(
Uri.encodeFull(lifeNews),
),
http.get(
Uri.encodeFull(electionNews),
),
http.get(
Uri.encodeFull(educationNews),
),
];
List responses = await Future.wait(responseFutures);
List data = jsonDecode(responses[0].body)['articles'];
List topNewsData = jsonDecode(responses[1].body)['stories'];
List businessNewsData = jsonDecode(responses[2].body)['items'][0]['stories'];
List worldNewsData = jsonDecode(responses[3].body)['items'][0]['stories'];
List sportsNewsData = jsonDecode(responses[4].body)['items'][0]['stories'];
List entertainmentNewsData = jsonDecode(responses[5].body)['items'][0]['stories'];
List tvNewsData = jsonDecode(responses[6].body)['items'][0]['stories'];
List lifeNewsData = jsonDecode(responses[7].body)['items'][0]['stories'];
List electionsNewsData = jsonDecode(responses[8].body)['items'][0]['stories'];
List educationNewsData = jsonDecode(responses[9].body)['items'][0]['stories'];
setState(() {
newsData = data;
topNews1 = topNewsData;
businessNews1 = businessNewsData;
worldNews1 = worldNewsData;
sportsNews1 = sportsNewsData;
entertainmentNews1 = entertainmentNewsData;
tvnews1 = tvNewsData;
lifeNews1 = lifeNewsData;
electionNews1 = electionsNewsData;
educationNews1 = educationNewsData;
isLoading = false; //this is for the initial loading, this is taking too much of time.
});
}
Related
Getting "spc message cannot be null" as response every time while providing implementation of Shaka player to play fairplay content on safari browser.Tried many ways to provide spc message in body and header also and we are actually sending it that i can see in network tab nut still cant find a solution. Here is the code below.
if (this.platform.getBrowserPlatform() === Constants.PLATFORMS.SAFARI_WEB) {
this.shakaPlayer.configure({
drm: {
servers: {
'com.apple.fps.1_0': `${this.config.baseUrl}${Constants.DRM_FAIRPLAY_LICENSE}`,
},
advanced: {
'com.apple.fps.1_0': {
serverCertificate: cert,
},
},
},
});
let that = this //,licenseUri;
this.shakaPlayer.configure('drm.initDataTransform', (initData) => {
const skdUri = shaka.util.StringUtils.fromBytesAutoDetect(initData);
var contentId = skdUri.substring(skdUri.indexOf('skd://') + 6);
// licenseUri = skdUri.replace('skd://', 'https://');
const url = new URL(contentId);
const urlParams = new URLSearchParams(url.search);
const cert = that.shakaPlayer.drmInfo().serverCertificate;
let id = urlParams.get('contentId');
that.id = id;
return shaka.util.FairPlayUtils.initDataTransform(initData, id, cert);
// let skdUrl = shaka.util.StringUtils.fromBytesAutoDetect(initData);
// licenseUri = skdUrl.replace('skd://', 'https://');
// const cert = that.shakaPlayer.drmInfo().serverCertificate;
// return shaka.util.FairPlayUtils.initDataTransform(initData, licenseUri, cert);
});
this.shakaPlayer.getNetworkingEngine().registerRequestFilter((type, request) => {
if (type != shaka.net.NetworkingEngine.RequestType.LICENSE) {
return;
}
let token = localStorage.getItem('auth');
let testToken = JSON.parse(token);
const originalPayload = new Uint8Array(request.body);
const base64Payload = shaka.util.Uint8ArrayUtils.toBase64(originalPayload);
const params = `{ "spc": "${base64Payload}", "assetId":"${that.id}"}`;
request.body = shaka.util.StringUtils.toUTF8(params);
request.headers['Content-Type'] = 'application/json';
request.headers['Authorization'] = `JWT ${testToken.access_token}`
console.log("request.body", request.body)
});
this.shakaPlayer.getNetworkingEngine().registerResponseFilter((type, response) => {
if (type != shaka.net.NetworkingEngine.RequestType.LICENSE) {
return;
}
console.log("license passed")
let responseText = shaka.util.StringUtils.fromUTF8(response.data);
responseText = responseText.trim();
if (responseText.substr(0, 5) === '<ckc>' &&
responseText.substr(-6) === '</ckc>') {
responseText = responseText.slice(5, -6);
}
response.data = shaka.util.Uint8ArrayUtils.fromBase64(responseText).buffer;
});
this.shakaPlayer.load(this.getProgramUrl(channel, program, restart)).then(() => {
console.log('1', this.shakaPlayer.isTextTrackVisible());
console.log('2', this.shakaPlayer.getTextTracks());
console.log('3', this.shakaPlayer.getTextLanguages());
}).catch((error) => {
console.log(error);
});
Smooth play of fairplay content on safari or some advise what can i do in this case
I want to fetch data from a REST API and store the information in my local database but get errors like :
_TypeError (type '(Map<String, dynamic>) => QuizDescription' is not a subtype of type '(dynamic) => QuizDescription' of 'f')
My List model:
factory QuizDescription.fromJson(Map<String, dynamic> json) =>
QuizDescription(
quizID: json['id'],
quizname: json['quizname'],
kurzbeschreibung: json['kurzbeschreibung'],
beschreibung: json['beschreibung'],
preis: json['preis'],
quizbild: json['quizbild']['url'],
rating: json['_rating_of_quiz']['rating_rating'],
);
Map<String, dynamic> toJson() => {
"quizID": quizID,
"quizname": quizname,
"kurzbeschreibung": kurzbeschreibung,
"beschreibung": beschreibung,
"preis": preis,
"quizbild": quizbild,
"rating": rating,
};
Database-Create method where I want to store information in :
static Future<int> createQuiz(QuizDescription quiz) async {
Database db = await DBQUIZProvider.initQuizDB();
return await db.insert('quiz', quiz.toJson());
}
API Call - which is using the Database method:
var url = Uri.parse('${Constants.BASE_URL}/QUIZ/getall');
var headers = {'Content-Type': 'application/json'};
var res = await http.get(
url,
headers: headers,
);
final body = json.decode(res.body);
QuizDescription quizliste =
body.map<QuizDescription>(QuizDescription.fromJson).toList();
DBQUIZProvider.createQuiz(quizliste);
try this...
var url = Uri.parse('${Constants.BASE_URL}/QUIZ/getall');
var headers = {'Content-Type': 'application/json'};
var res = await http.get(url, headers: headers,);
List tempList = json.decode(res.body);
Database-Create method where store information in
static Future<int> createQuiz(List quiz) async {
int result = 0;
Database db = await DBQUIZProvider.initQuizDB();
for (var element in planets) {
QuizDescription quizD = QuizDescription. fromJson(element);
result = await db.insert(
'quiz',
quizD.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
return result;
}
try this:
static Future<int> createQuiz(Map<String, dynamic> row) async {
Database db = await DBQUIZProvider.initQuizDB();
return await db.insert('quiz', row);
}
I am running into a bit of a snag setting up pagination in Google Apps Script. I am trying to use it for Shopify API. Reference links attached.
I attached the code below of what I have so far -
trying to figure out how to use the "While" statement to make it check if there is a Next Page URL
trying to figure out a way to parse the Link in the header. Example below. On pages 2+ there will be a next and previous link. We only need the next
https://shop-domain.myshopify.com/admin/api/2019-07/products.json?limit=50&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6MTk4NTgyMTYzNCwibGFzdF92YWx1ZSI6IkFjcm9saXRoaWMgQWx1bWludW0gUGVuY2lsIn0%3D; rel="previous", https://shop-domain.myshopify.com/admin/api/2019-07/products.json?limit=50&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6MTk4NTgzNjU0NiwibGFzdF92YWx1ZSI6IkFoaXN0b3JpY2FsIFZpbnlsIEtleWJvYXJkIn0%3D; rel="next
function callShopifyOrderCount() {
var accessToken = 'xxxx';
var store_url = 'https://xxxx.myshopify.com/admin/api/2021-01/orders.json?status=any&fields=created_at,id,name,total-price&limit=20';
var headers = {
"Content-Type": "application/json",
'X-Shopify-Access-Token': accessToken
};
var options = {
"method": "GET",
"headers": headers,
"contentType": "application/json",
"muteHttpExceptions": true,
}
var response = UrlFetchApp.fetch(store_url, options)
// Call the link header for next page
var header = response.getHeaders()
var linkHeader = header.Link;
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody);
var objectLength = responseJson.orders.length;
for (var i = 0; i < objectLength; i++) {
var orderId = responseJson.orders[i].id;
var orderPrice = responseJson.orders[i].total_price;
var orderName = responseJson.orders[i].name;
}
} else {
Logger.log(
Utilities.formatString(
"First Request failed. Expected 200, got %d: %s",
responseCode,
responseBody
)
);
// ...
}
// *** NEED TO FIGURE OUT WHILE STATEMENT //
while (Link != null) {
var store_url = linkHeader;
var response = UrlFetchApp.fetch(store_url, options)
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
var header = response.getHeaders()
var linkHeader = header.Link;
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody);
var objectLength = responseJson.orders.length;
for (var i = 0; i < tweetLength; i++) {
var orderId = responseJson.orders[i].id;
var orderPrice = responseJson.orders[i].total_price;
var orderName = responseJson.orders[i].name;
}
}
else {
Logger.log(
Utilities.formatString(
"Second Request failed. Expected 200, got %d: %s",
responseCode,
responseBody
)
);
}
}
}
References:
https://shopify.dev/tutorials/make-paginated-requests-to-rest-admin-api
https://www.shopify.com/partners/blog/relative-pagination
In this method, the output is as follows:
I/flutter ( 2928): 200
I/flutter ( 2928): null
I/flutter ( 2928): [Instance of 'Images']
This is causing snapshot.data to be null in my FutureBuilder as well.Any Idea why is this happening?
Future<List<Images>> getData( File f ) async {
List<Images> list;
// String link = "https://clothest.herokuapp.com/";
String link ="https://us-central1-velvety-rookery-274308.cloudfunctions.net/function-1";
var stream = new http.ByteStream(DelegatingStream.typed(f.openRead()));
var length = await f.length();
var postUri = Uri.parse(link);
var request = new http.MultipartRequest("POST", postUri);
var multipartFileSign = new http.MultipartFile('File', stream, length,
filename: basename(f.path));
request.files.add(multipartFileSign);
request.headers.addAll({"content-type": "application/json"});
var response = await request.send();
print(response.statusCode); //200 OK
if (response.statusCode == 200){
response.stream.transform(utf8.decoder).listen((value) {
var data = json.decode(value);
var rest = data["Items"] as List;
list = rest.map<Images>((json) => Images.fromJson(json)).toList();
print(list.toString());
});
}
print(list.toString());
return list;
}
So I managed to solve this problem by using another package instead of the HTTP package. Dios package is the best regarding HTTP requests. Updated code:
Future<List<Images>> getData( File f ) async {
List<Images> lst=[];
String link ="https://us-central1-velvety-rookery-274308.cloudfunctions.net/function-1";
Dio dio = new Dio();
FormData formdata = new FormData(); // just like JS
formdata.add('File', new UploadFileInfo(f, basename(f.path)));
await dio.post(link, data: formdata, options: Options(
method: 'POST',
responseType: ResponseType.JSON // or ResponseType.JSON
)).then((response) {
print(response);
if(response.statusCode==200){ //OK
var responseBody = response.data; //Map
var rest = responseBody["Items"] as List;
//print(rest);
lst = rest.map<Images>((json) => Images.fromJson(json)).toList(); //instances of 'Images' object
//print(lst);
}
}
).catchError((error) => print(error));
return lst;
For authentication I am using Auth0 AuthenticationApi. In Account Controller, I need to fetch the user_metadata but it's missing. Any alternative to fetch the user_metadata?
AuthenticationApiClient client = new AuthenticationApiClient(new Uri($"https://{_auth0Options.Domain}/"));
var authenticateResponse = await client.GetTokenAsync(new ResourceOwnerTokenRequest
{
ClientId = _auth0Options.ClientId,
ClientSecret = _auth0Options.ClientSecret,
Scope = "openid",
Realm = _auth0Options.Connection,
Username = vm.EmailAddress,
Password = vm.Password
});
var user = await client.GetUserInfoAsync(authenticateResponse.AccessToken);
if (user.UserMetadata != null)
{
// Giving error...any alternative to access the userMetaData ?
}
Yes, as far as I see it now, the legacy call still works. However, I don't have a non-legacy solution yet :(
using (var client = GetClient())
{
var jObject = new JObject(new JProperty("id_token", id_token));
var response = await client.PostAsJsonAsync("tokeninfo", jObject);
if (response.IsSuccessStatusCode)
{
var userProfileJson = JObject.Parse(await response.Content.ReadAsStringAsync());
retVal.user_id = userProfileJson.Value<string>("user_id");
retVal.email = userProfileJson.Value<string>("email");
retVal.user_name = userProfileJson.Value<string>("nickname");
if (userProfileJson.Value<string>("created_at") != null)
{
retVal.created_at = userProfileJson.Value<DateTime>("created_at");
}
var exists = userProfileJson.TryGetValue("user_metadata", out JToken meta);