PathNotFoundException: Cannot open file 'assets/certs/ca_bundle.crt' in Flutter on Android

I'm trying to build flutter application, and I'm encountering an error when running the app on Android. The same code works perfectly fine on iOS. The error message is: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PathNotFoundException: Cannot open file, path = 'assets/certs/ca_bundle.crt' (OS Error: No such file or directory, errno = 2)

The error is happening in the following class:

import 'dart:io';
import 'package:grpc/grpc.dart';
import 'package:injectable/injectable.dart';
import 'package:test/src/core/secure/secure_storage_service.dart';
import 'package:test/src/features/auth/data/interceptor/auth_interceptor.dart';
import '../../../../generated/user.pbgrpc.dart';

@Injectable()
class UserServiceHandler extends UserServiceClient {
  UserServiceHandler(SecureStorageService storageService)
      : super(
          ClientChannel(
            _host,
            port: 50051,
            options: ChannelOptions(
              credentials: ChannelCredentials.secure(
                certificates: File('assets/certs/ca_bundle.crt').readAsBytesSync(),
              ),
            ),
          ),
          interceptors: [AuthInterceptor(storageService)],
          options: CallOptions(
            timeout: const Duration(seconds: 30),
          ),
        );
  static String get _host {
    return 'localhost';
  }
}

In my pubspec.yaml, I have listed the assets like this:

flutter:
  uses-material-design: true
  assets:
    - assets/certs/
    - assets/translations/
    - assets/
    - assets/icons/
    - RiveAssets/

I'm not sure why the file cannot be found on Android but works fine on iOS.

Upvotes: 1

Views: 72

Answers (1)

albert
albert

Reputation: 212

From the error message and from the code you've provided, The path you are trying to access is assets/crt/ca_bundle.crt but in pubspec.yaml, you've mentioned something different , which is assets/certs/

There are chances of a typo. The folder you are targeting is different in both the cases. Fix the typo or add the correct folder, which is assets/crt/ in pubspec.yaml. The issue will be resolved.

See the difference between assets/crt/ca_bundle.crt and assets/certs/

Upvotes: 0

Related Questions