isar / isar/hive

Unhandled Exception: FileSystemException: Cannot create file, path

Open
#153 12 comments 0 reactions 1 assignee Claimed by @simc View on GitHub
bug
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Steps to Reproduce**
Upgraded previously working app using HiveDB to Hive 1.2.0

- https://github.com/mdrideout/flutter_hive_provider_example

**Error**
```
Syncing files to device iPhone 11 Pro Max...
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Cannot create file, path = '/Users/matt/Library/Developer/CoreSimulator/Devices/03D89D69-AF9B-4C27-B92E-52324FE97F34/data/Containers/Data/Application/D0964550-86ED-43BE-A833-41BE76271E58/Documents/contactsbox.hive' (OS Error: No such file or directory, errno = 2)
#0 _File.create. (dart:io/file_impl.dart:265:9)
#1 _rootRunUnary (dart:async/zone.dart:1134:38)
#2 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
#3 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
#4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
#5 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
#6 Future._completeWithValue (dart:async/future_impl.dart:524:5)
#7 Future._asyncComplete. (dart:async/future_impl.dart:554:7)
#8 _rootRun (dart:async/zone.dart:1126:13)
#9 _CustomZone.run (dart:async/zone.dart<…>
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Cannot create file, path = '/Users/matt/Library/Developer/CoreSimulator/Devices/03D89D69-AF9B-4C27-B92E-52324FE97F34/data/Containers/Data/Application/D0964550-86ED-43BE-A833-41BE76271E58/Documents/contactsbox.hive' (OS Error: No such file or directory, errno = 2)
#0 _File.create. (dart:io/file_impl.dart:265:9)
#1 _rootRunUnary (dart:async/zone.dart:1134:38)
#2 _CustomZone.runUnary (dart:async/zone.dart:1031:19)
#3 _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
#4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
#5 Future._propagateToListeners (dart:async/future_impl.dart:709:32)
#6 Future._completeWithValue (dart:async/future_impl.dart:524:5)
#7 Future._asyncComplete. (dart:async/future_impl.dart:554:7)
#8 _rootRun (dart:async/zone.dart:1126:13)
#9 _CustomZone.run (dart:async/zone.dart<…>
```

#Code sample
**main.dart**
```
import 'package:flutter/material.dart';
import 'package:flutter_hive_example/models/contact.dart';
import 'package:flutter_hive_example/screens/add_contact_screen.dart';
import 'package:provider/provider.dart';
import 'package:flutter_hive_example/models/contact_data.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_hive_example/screens/contacts_screen.dart';

void main() {
Hive.registerAdapter(ContactAdapter(), 0);
runApp(MyApp());
}

Future _initHive() async {
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
builder: (context) => ContactsData(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
'/': (context) => FutureBuilder(
future: _initHive(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.error != null) {
print(snapshot.error);
return Scaffold(
body: Center(
child: Text('Error initializing hive data store.'),
),
);
} else {
return ContactsScreen();
}
} else {
return Scaffold();
}
},
),
'/AddContactScreen': (context) => AddContactScreen(),
},
),
);
}
}
```
**contact.dart**
```
/**
* Our contact data model
*/

import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';

part 'contact.g.dart';

@HiveType() // use Hive to generate a type adapter
class Contact {
// Define variables

@HiveField(0)
final String name;

@HiveField(1)
final String phone;

@HiveField(2)
final String email;

// Constructor
Contact({@required this.name, this.phone, this.email});
}
```

**contact_data.dart**
```
/**
* Manages the data for our contacts
*/

import 'package:flutter/foundation.dart';
import 'contact.dart';
import 'package:hive/hive.dart';

class ContactsData extends ChangeNotifier {
// Name our hive box for this data
String _boxName = "contactsBox";

// Initialize our list of contacts
List _contacts = [];

/// Get Contacts
/// Gets all contacts from the hive box and loads them into our state List
void getContacts() async {
var box = await Hive.openBox(_boxName);

// Update our provider state data with a hive read, and refresh the ui
_contacts = box.values.toList();
notifyListeners();
}

/// Get Contact
/// Retrieves a specific contact from our state
Contact getContact(index) {
return _contacts[index];
}

/// Contact Count
/// Returns the length of the contact array
int get contactCount {
return _contacts.length;
}

/// Add Contact
/// - Saves contact data to Hive box persistent storage
/// - Updates our List with the hive data by read
/// - Notifies listeners to update the UI, which will be a consumer of the _contacts List
void addContact(Contact newContact) async {
var box = await Hive.openBox(_boxName);

// Add a contact to our box
await box.add(newContact);

// Update our provider state data with a hive read, and refresh the ui
_contacts = box.values.toList();
notifyListeners();
}

/// Delete Contact
/// - Deletes the contact from Hive
/// - Updates our List from Hive
/// - Notifies listeners to update the UI
void deleteContact(index) async {
var box = await Hive.openBox(_boxName);

print("Box Keys: " + box.keys.toList().toString());
print("Deleting Index " +
index.toString() +
" which has a box key value of: " +
box.keys.toList()[index].toString());

// Get key of box we want to delete
var boxKey = box.keys.toList()[index];

// Delete the contact from the box
await box.delete(boxKey);

// Update our provider state data with a hive read, and refresh the ui
_contacts = box.values.toList();
notifyListeners();
}
}
```

**Version**
- Platform: iOS, Android, Mac, Windows, Linux, Web
- Flutter version: Flutter 1.12.13+hotfix.5 • channel stable
- Hive version: 1.2.0

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.