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

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.

Related

Login with facebook works on real phones but not emulators, it started happening with the new emulator update

This code works on real phones but not on emulators
I use flutter_facebook_auth. This exact code worked fine before emulator update and it's still works on real devices, but on emulator it just do nothing after coming back from fb login page.
static Future<User?> signInWithFacebook() async {
try {
//Trigger the sign-in flow
final LoginResult loginResult =
await FacebookAuth.instance.login(permissions: [
'public_profile',
'email',
'user_friends',
]);
if (loginResult.status == LoginStatus.success) {
print('success');
facebookAuthCredential =
FacebookAuthProvider.credential(loginResult.accessToken!.token);
print(loginResult.accessToken?.token);
Credential = await FirebaseAuth.instance
.signInWithCredential(facebookAuthCredential);
print('here 1');
UserData = await FacebookAuth.instance.getUserData(
fields:
"name,email,picture.width(400),birthday,friends,gender,link");
print(UserData);
} else {
print(loginResult.message);
error = loginResult.message!;
}
user = Credential.user;
await user
?.updatePhotoURL(UserData['picture']['data']['url'])
.then((value) {
print('success' + UserData['picture']['data']['url']);
});
return Credential.user;
} on FirebaseException catch (e) {
error = e.code;
print(e.code);
}
}
}
In the above code the app flow stops or freezes on
Credential = await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
What I have tried is:
Tried other emulators, different api's and models.
I tried adding new release and debug keyhashes to both facebook and firebase
I tried increasing emulator ram and cpu cores etc
I tried refreshing fb sdk and json and other dev dependencies
I tried flutter clean
I tried Invalidate Caches
I uninstalled android studio and all of it's components and install again
Is this happening with just me or it's a bug in the new emulator update?
It's been 12 hours, can somebody please help and check if their updated emulator is working with fb login
I solved it by downgrading emulator version to 31.2.9,
Here is emulator_archive how you can do it:

Is it safe to use print() with dart?

Is it safe to still use print() in dart (flutter 2.5.1), if not what command should I use to print errors?
if (query != null) {
_query.addAll(query);
}
return await dio.get(_url, queryParameters: _query);
} on DioError catch (e) {
print('DioError:$e');
}
What you are getting is just a warning. You should throw errors instead of printing it.
if (query != null) {
_query.addAll(query);
}
return await dio.get(_url, queryParameters: _query);
} on DioError catch (e) {
throw Exception(e);
}
If you're inside a widget ,
Use debugPrint('data') , because it'll only show data in debug mode and will not run in release mode. So, it'll save you a lot of time.
Otherwise , you can also use log('data') , it'll show whole response in logging pattern in console.

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

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.

How to prevent flutter app from crashing after Dio error?

I am using the flutter-dio package to make HTTP requests and have made a wrapper on it from my convenience like so
Future<dynamic> post(String uri, dynamic body, {String authKeyCust}) async {
authKey = await SecureStorage.getAuthKey();
try {
Response response = await dio.post(
uri,
data: body,
options: Options(headers: {"authkey": authKeyCust ?? "$authKey"}),
);
return response.data;
} on DioError catch (error) {
print('error: $error');
} on SocketException catch(error) {
print('No net');
}
}
All I want is the app to not crash/pause on exception when any error occurs.
I also had the same problem.
First, try to run it from command prompt with flutter run see if there is occurring any problem or not.
If the command prompt is showing no problem & your app is running smoothly then you have to check the IDE. If you are using VSCode then switch to Debug is side bar, see which options are ticked in Breakpoint section.
If All Exceptions is ticked then the debugger will pause on every exception. Uncheck both All Exceptions & Uncaught Exceptions then try refresh restart.
Hope this will solve your problem.

Flutter getting an exception when calling queryPastPurchases (in_app_purchase plugin): Cannot find receipt for the current main bundle

I'm trying to utilise the in_app_purchase plugin (https://pub.dev/packages/in_app_purchase) for my Flutter App.
Calling a queryProductDetails(ids) works just fine,
but when calling the queryPastPurchases() I get the following exception:
PlatformException (PlatformException(storekit_no_receipt, Cannot find receipt for the current main bundle., null))
Seems like this is the source for the exception:
https://github.com/flutter/plugins/blob/master/packages/in_app_purchase/ios/Classes/FIAPReceiptManager.m
Worth mentioning:
I've setup an IAP in App-Store-Connect.
This IAP hasn't been sent for Apple approval yet.
App is in development, tested using TestFlight with beta testers.
Another issue is that this exception doesn't get cought in the try-catch statement (see code below), but only by the environment when checking the "All Exceptions" option in VSCode...
Tested on Emulator and real device.
[UPDATE]:
Added a breakpoint to the objective-c code of the plugin, and saw that the receiptURL refers to "sandboxReceipt". So maybe it's because I don't use sandbox users?
Future<void> _getPastPurchases() async {
try {
final QueryPurchaseDetailsResponse response = await InAppPurchaseConnection.instance.queryPastPurchases();
if (response.error != null) {
// Handle the error
print(response.error.toString());
}
print('response = ' + response.toString());
for (PurchaseDetails purchase in response.pastPurchases) {
if (Platform.isIOS) {
//InAppPurchaseConnection.instance.completePurchase(purchase);
}
}
setState(() {
_purchases = response.pastPurchases;
});
} on PlatformException catch(err) {
print(err.toString());
}
catch (err) {
print(err.toString());
}
}