Skip to content

Latest commit

 

History

History
36 lines (24 loc) · 1.07 KB

auto_increment.md

File metadata and controls

36 lines (24 loc) · 1.07 KB

Auto increment & indices

We already know that Hive supports unsigned integer keys. You can use auto-increment keys if you like. This is very useful for storing and accessing multiple objects. You can use a Box like a list.

import 'package:hive/hive.dart';

void main() async {
  var friends = await Hive.openBox('friends');
  friends.clear();

  friends.add('Lisa');            // index 0, key 0
  friends.add('Dave');            // index 1, key 1
  friends.put(123, 'Marco');      // index 2, key 123
  friends.add('Paul');            // index 3, key 124

  print(friends.getAt(0));
  print(friends.get(0));
  
  print(friends.getAt(1));
  print(friends.get(1));
  
  print(friends.getAt(2));
  print(friends.get(123));
  
  print(friends.getAt(3));
  print(friends.get(124));
}

There are also getAt(), putAt() and deleteAt() methods to access or change values by their index.

By default, String keys are sorted lexicographically and they have also indices.

?> Even if you only use auto increment keys, you should not rely on keys and indices being the same.