skip to Main Content

my flutter application does not send requests to my server in the real android emulator or simulator I don’t think there is a problem on the server side because it works normally on ios in the same way I can send requests with postman I tried the ways I saw on the internet but it didn’t get better my android manifest file is like this thanks for your help in advance

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:usesCleartextTraffic="true"
        android:label="seederzone"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>

</manifest>

2

Answers


  1. Chosen as BEST ANSWER

    **I solved the problem, maybe there will be those who experience the same error, I am writing the solution here, since my website, which I send requests with api, uses ssl, this cannot be verified on the android side and did not send requests to the server. To fix this, I added my ssl certificate to my assets and added the following to the main file of my application **

      final ByteData data =
          await PlatformAssetBundle().load('assets/cert/my_cert.pem');
      SecurityContext.defaultContext
          .setTrustedCertificatesBytes(data.buffer.asUint8List());
    

    The codes you need to add are as I mentioned above

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      final ByteData data =
          await PlatformAssetBundle().load('assets/cert/my_cert.pem');
      SecurityContext.defaultContext
          .setTrustedCertificatesBytes(data.buffer.asUint8List());
      await EasyLocalization.ensureInitialized();
      final appDocumentDir = await path_provider.getApplicationDocumentsDirectory();
      Hive.init(appDocumentDir.path);
      await Hive.openBox<String>("AppDatabase");
      await initializeDateFormatting('tr_TR').then((_) {
        runApp(
          EasyLocalization(
            supportedLocales: AppConstants.SUPPORTED_LOCALES,
            path: AppConstants.LANG_PATH,
            child: const MyApp(),
          ),
        );
      });
    }
    

  2. It should be enough, iOS doesn’t require that but Android does.

    Make sure after you save the updated AndroidManifest.xml you perform a full reinstall on your device, by running these at your project folder:

    flutter clean
    flutter pub get
    flutter run --release
    

    ps:
    You can also try this simple code to check if it really is a internet permission problem or if it is your requisition (add the http package and run on release mode):

    import 'package:flutter/material.dart';
    import 'dart:convert';
    import 'package:http/http.dart' as http;
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String _displayText = '';
    
      Future<void> _fetchData() async {
        final url = 'https://dummyjson.com/carts';
        try {
          final response = await http.get(Uri.parse(url));
          if (response.statusCode == 200) {
            String result = response.body;
            setState(() {
              _displayText = result.substring(0, 100);
            });
          } else {
            setState(() {
              _displayText = 'Failed to load data from the URL.';
            });
          }
        } catch (e) {
          setState(() {
            _displayText = 'Error: ${e.toString()}';
          });
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  _displayText,
                  style: TextStyle(fontSize: 16),
                ),
                SizedBox(height: 20),
                ElevatedButton(
                  onPressed: _fetchData,
                  child: Text('Fetch Data'),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search