Flutter Dio/Retrofit getting connectTimeout on latest versions of android

41 views Asked by At

A very strange thing happened while I was developing an app using Flutter. The code below worked flawlessly on all versions of Android, which is very strange.

A few days ago, this implementation for receiving data from the web service stopped working. When I investigated further, I realized that this problem occurred on higher versions of Android. It seems that this issue has arisen after upgrading Flutter and Dart, as I get a connectTimeout error when debugging the app and receiving data from the web service.

AndroidManifest:

<manifest xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:name="${applicationName}"
        android:icon="@mipmap/launcher_icon"
        android:label="HazaraPedia"
        android:networkSecurityConfig="@xml/network_security_config"
        android:allowBackup="true"
        android:appCategory="social"
        android:usesCleartextTraffic="true"
        tools:targetApi="o">

pubspec.yaml:

environment:
  sdk: '>=3.3.0 <4.0.0'

dio: ^5.4.1
retrofit: ^4.1.0
retrofit_generator: ^8.1.0

flutter doctor -v:

flutter doctor -v
[✓] Flutter (Channel stable, 3.19.0, on macOS 14.2.1 23C71 darwin-arm64, locale en-US)
    • Flutter version 3.19.0 on channel stable at /Users/mahdipishguy/Desktop/home/develop/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (7 days ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/mahdipishguy/Desktop/home/develop/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/mahdipishguy/Desktop/home/develop/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.2

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (1 available)
    • macOS (desktop) • macos • darwin-arm64 • macOS 14.2.1 23C71 darwin-arm64

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Retrofit with multiple endpoint support using provider:

@RestApi(baseUrl: Server.baseApiUrl)

abstract class RestClient {

  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  @GET(Server.wpCategories)
  Future<List<CategoryDto>> getCategories(
    @DioOptions() options,
  );


  @GET(Server.wpAppCategoriesPosts)
  Future<List<PostDto>> getAppCategories(
    @Query('page') int page,
    @DioOptions() options,
  );


  static RestClient create() {
    final BaseOptions options = BaseOptions(
      connectTimeout: const Duration(seconds: 60),
      receiveTimeout: const Duration(seconds: 90),
    );

    final cacheManager = DioCacheManager(CacheConfig(baseUrl: Server.serverUrl));

    final dio = Dio(options)
      ..interceptors.addAll(
        [
          cacheManager.interceptor,
          PrettyDioLogger(
            requestHeader: true,
            requestBody: true,
            responseBody: false,
            responseHeader: true,
            // error: true,
            // compact: true,
            maxWidth: 110,
          ),
        ],
      );

    dio.interceptors.add(
      InterceptorsWrapper(
        onRequest: (RequestOptions request, RequestInterceptorHandler handler) {
          options.headers['Content-Type'] = 'application/json; charset=utf-8';
          options.headers['Accept'] = 'application/json';

          return handler.next(request);
        },
        onError: (DioException error, ErrorInterceptorHandler handler) async {
          print(error);

          return handler.next(error);
        },
      ),
    );
    return _RestClient(dio);
  }
}

and my Server class;

class Server {
  static const String serverUrl = 'https://hazarapaedia.org';
  static const String baseApiUrl = '$serverUrl/wp-json/wp/v2';

  static const String wpCategoryPosts = '$baseApiUrl/posts?_embed&order=asc&orderby=title&per_page=25&&order=asc';

  static const String wpAppCategoriesPosts = '$baseApiUrl/posts?categories=311,312,315,312,313,307,306,316,317,318,310,319,314,308,309&per_page=25&_embed&orderby=title&order=asc';
}
0

There are 0 answers