0

I am working on a CRUD application. Problem : everything works perfectly when I debug my application on the emulator or physical device but when I create APK and run it on mobile all request works fine except post request.

As I search I have given internet permission already.

My post request

await http.post(
  Uri.parse('https://myap'),
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Bearer ${token}',
  },
  body: jsonBody,
).then(
  (response) {
    if (response.statusCode == 201) {
      navigatorKey.currentState?.pushNamed('home');
    }
 
  },
).catchError(
  (e) {
        print(e);
  },
);

Get function fine on apk, emulator, and physical devices.

 Future fetchFeed() async {
    await http.get(Uri.parse('https://myapi'), headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token',
    }).then((response) {
      if (response.statusCode == 200) {
        print(response.statusCode);
      }
    }).catchError(
      (e) {
          print(e);
      },
    );
  }
DmDev
  • 592
  • 1
  • 5
  • 17

2 Answers2

1

If it works properly on debug mode but not in release(apk) mode, there's chances of lack of internet permissions in manifest.xml file.

Android apps must declare their use of the internet in the Android manifest (AndroidManifest.xml), at "android\app\src":

Here you will find 3 folders, add this snippet to AndroidManifest in each folder.

<manifest xmlns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <application ...
</manifest>
Delwinn
  • 891
  • 4
  • 19
0

The error is in the jsonBody. It does not consider it as map so it is still trying to send the request,

Try to put :

    body : { 
       'key1' : '${jsonBody['key1']}',
       'key2' : '${jsonBody['key2']}',
       'key3' : '${jsonBody['key3']}',
   
    }

It will work then you can leave it or you can search what is the problem in jsonBody

Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
møđ ef
  • 21
  • 4