flutter webapp is not working but iOS and Apk files are working - flutter

Developing a single webapp with http package
iOS and apk files are working fine, but webapp is not updating content .. Image is attached below
Future<Exercise> fetchSensorValues() async {
final _sensorAPIurl = 'my_url';
var response = await http.get(Uri.parse(_sensorAPIurl));
if(response.statusCode == 200){
print(response.body);
final exercise = exerciseFromJson(response.body);
return exercise ;
}else {
print("EROOR") ;
throw Exception('Failed to load data');
}
}
``[sample image][1]
[1]: https://i.stack.imgur.com/GbB2p.jpg

In Web app you need to load https content in order to work.
Make sure you are getting content in https and not in http.

Related

Flutter Chrome Debugging: API calls don't work at all

I'm developing a simple web/Android Flutter App. Everything works fine on Android, but when I try debugging it on Chrome (web-javascript), none of my API calls work.
Debugger stops in ticker.dart > TickerFuture > _complete() and this message appears in the console, and the API call continues forever.
ChromeProxyService: Failed to evaluate expression '_primaryCompleter'
I've tried using CORS and try catch blocks to see if i can get more info about the error.
This is an example API call:
Future<bool> login(LoginData l) async {
try {
Uri url = Uri.parse(baseUrl + "/login/");
final response = await http.post(url,
body: {"email": l.name, "password": l.password},
headers: {"Access-Control-Allow-Origin": "*"});
if (response.statusCode == 200) {
return response.body == "true";
} else {
throw Exception('Failed to load books');
}
} catch (e) {
print(e);
throw Exception('Failed to load books');
}
}
There were some issues with debugging in Chrome after Chrome v100 released.
Flutter 2.10.5 was released to fix some of these issues:
https://github.com/flutter/flutter/wiki/Hotfixes-to-the-Stable-Channel#2105-april-18-2022
flutter/101224 - Flutter web debugger fails when using chrome 100 or greater.

Flutter - cannot download audio files

It's been 3 days that I try to fix an issue with the download of audio files with my Flutter application. When I try to download audio files, the request keep the "pending" status and finish with no error.
I have research a lot and find something about the contentLength of the client who is always at 0 but it doesn't help.
Now I have tried to make a get request to a website with sample audio files and it doesn't work too. I have tested via Postman and it always work.
My function:
Future<void> _download(String url, String filepath) async {
final response = await this.get("https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3");// await this.get("$baseURL$url");
log("Try to get audio files: ${response.isOk}");
if (response.isOk) {
File file = File(filepath);
final raf = file.openSync(mode: FileMode.write);
response.bodyBytes.listen((value) {
raf.writeFromSync(value);
}, onDone: () {
log("closed $filepath");
raf.closeSync();
});
}
}
The response.isOk is always false.
I used GetConnect from GetX package who used httpClient.
Via Dart devtools I obtain this from the request:
https://prnt.sc/1q3w33z
https://prnt.sc/1q3x9ot
So I used another package: Dio and now it works.

Api is returning media data in response.body in Flutter

I am using api in flutter that is returning media data so how to handle this.
ScreenShot of my code
Post Man screenshot
Thanks in advance for your precious time.
Save your response to temp file or application document directory and add Image.file widget to your project to show this downloaded file. to save file you can use path_provider package.
if(response.statusCode == 200) {
var bytes = await consolidateHttpClientResponseBytes(response);
filePath = '$dir/$fileName';
file = File(filePath);
await file.writeAsBytes(bytes);
}
else
filePath = 'Error code: '+response.statusCode.toString();
}
also check this stackoverflow question and answers.

How to use Steam (OpenId) to login into my Firebase powered Flutter app?

I'm building a Flutter app and I want users to be able to login through Steam (OpenId).
The app is powered by Firebase.
I'm pretty new to the whole OpenId world and I've read into it but I'm still not getting it.
I found that Firebase Supports signinWithCustomToken so I think I should get the Steam OpenId to get me one of those.
I also found two packages which might help me but I couldn't get those to work either:
https://pub.dev/packages/openid_client
https://pub.dev/packages/steam_login
If tried the following sample from the openid_client package:
_authenticate(Uri uri, String clientId, List<String> scopes) async {
try {
var issuer = await Issuer.discover(uri);
var client = Client(issuer, clientId);
print('CLAIMS:');
print(issuer.claimsMap);
urlLauncher(String url) async {
if (await canLaunch(url)) {
await launch(url, forceWebView: true);
} else {
throw 'Could not launch $url';
}
}
var authenticator = Authenticator(client,
scopes: scopes, port: 4000, urlLancher: urlLauncher);
var c = await authenticator.authorize();
closeWebView();
print(await c.getUserInfo());
}catch (err) {
print(err);
}
}
But I have no idea what to give as the uri, clientId and scopes.
For the uri I've tried https://steamcommunity.com/openid but when I try this I'm getting an error that it tried to Json.parse a xml response.
I've also tried the example for the steam_login package here, but that does nothing and doesn't feel like it should work within a Flutter app (even though the package states it Flutter compatible).
So if anybody could explain to me how I could achieve this with a working sample that would be great.
If I get this to work I'm planning on creating a tutorial/guide for it for others.

Looking for a good sample working code for downloading any file from URL in flutter. If its with native downloader then this will be very good

Looking for a good sample working code for downloading any file from URL in flutter. If its with native downloader then this will be very good. Please help me with sample of code to download any file using native downloader in flutter.
I have used few libraries but didn't turned out well.
For a mobile device, I used the http package to download a file to the applications document directory
First, create a httpClient object
static var httpClient = new HttpClient();
Then you can create a function like this to download the file:
Future<void> _downloadFile({
required String fileName,
}) async {
String url = ...;
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory())!.path;
File file = new File('$dir/$fileName'); // Note: Filename must contain the extension of the file too, like pdf, jpg etc.
await file.writeAsBytes(bytes);
}
For a flutter web application, I felt the url_launcher package was the easiest to work with.
_launchURL() async {
String url = ...;
if (await canLaunch(url)) {
await launch(url);
print('URL Launcher success');
} else {
throw Exception('Could not launch $url');
}
}
The url_launcher package code works even for a mobile device but it opens a new browser window to download the required file which is not a good user experience so I have used 2 approaches for the same problem.
Hope I have answered your query.