swapnil mane
swapnil mane

Reputation: 257

Flutter - Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null,

I am trying to use Hive database in my flutter project. In order to use hive db I will have to use path_provider package. I installed that too. but when I run the app again or restart it is neither running nor restarting and getting this error.

enter image description here

 Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)

E/flutter (15905): #0
PathProviderApi.getApplicationDocumentsPath
package:path_provider_android/messages.g.dart:9

ANDROID MANIFEST FILE PERMISSIONS

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
   />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
  />

MY CODE

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final document = await getApplicationDocumentsDirectory(); // ERROR AT THIS LINE
  Hive.init(document.path);
  runApp(const MyApp());
}

Upvotes: 1

Views: 7058

Answers (3)

Igor Vasilev
Igor Vasilev

Reputation: 351

You probably need to initialize your Flutter plugins first, including the path_provider. This can be done by calling GeneratedPluginRegistrant.register with a FlutterPluginRegistry as a parameter. Examples of FlutterPluginRegistry conforming classes are FlutterAppDelegate / FlutterViewController on iOS, as well as FlutterEngine.

Upvotes: 0

Jakhongir Anasov
Jakhongir Anasov

Reputation: 1119

Actually, I didn't put any uses-permission in Maneifest file to use Hive. Instead of Hive.init(), I would like to suggest to use Hive.initFlutter(). But without

final document = await getApplicationDocumentsDirectory();

inside initFlutter() method, these actions are already done by itself.

Future<void> main() async {
 WidgetsFlutterBinding.ensureInitialized();
 Hive.initFlutter();
 runApp(const MyApp());
}

Upvotes: 1

Zeeshan Ayaz
Zeeshan Ayaz

Reputation: 1038

Adding permission in AndroidManifest.xml file is like defining all the permission you may use in your app. but to actually use those permissions, you need to ask user to allow those permissions like using internal/external storage. One way to do this is with permission_handler package.

Upvotes: 1

Related Questions