Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoincrement Key type and stringMapStoreFactory #304

Open
fvisticot opened this issue Feb 19, 2022 · 3 comments
Open

Autoincrement Key type and stringMapStoreFactory #304

fvisticot opened this issue Feb 19, 2022 · 3 comments

Comments

@fvisticot
Copy link

I don't know if it is a bug or not but
When using a stringMapStoreFactory, the key returned after a "store.add(_db, )" is of int type.
I was thinking the returned key type would by String.

The documentation is not clear on this topic.

Can you please advice ?

@alextekartik
Copy link
Collaborator

alextekartik commented Feb 20, 2022

That's definitely a bug. Yes the key returned should be a string. Do you have a way to reproduce it? Here is a simple test showing that it returns an int if the factory is intMapStoreFactory and a string if the factory is stringMapStoreFactory:

import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_memory.dart';
import 'package:test/test.dart';

void main() {
  test('add type', () async {
    var factory = newDatabaseFactoryMemory();
    var db = await factory.openDatabase('test_add');
    var intKeyStore1 = intMapStoreFactory.store('store1');
    var stringKeyStore2 = stringMapStoreFactory.store('store2');
    var key1 = await intKeyStore1.add(db, {'test': 'value1'});
    var key2 = await stringKeyStore2.add(db, {'test': 'value1'});

    // key1 should be an int, key2 should be a string
    expect(key1, const TypeMatcher<int>());
    expect(key2, const TypeMatcher<String>());
  });
}

@fvisticot
Copy link
Author

Version: sembast: ^3.1.2

The code:

DB init:

var appDirectory = await getApplicationDocumentsDirectory();
DatabaseFactory dbFactory = databaseFactoryIo;
_db = await dbFactory.openDatabase('${appDirectory.path}/beacons_location_v3.db');

Adding an item:

StoreRef storeRef = stringMapStoreFactory.store(kBeaconsStore);
final key = await storeRef.add(_db, beaconRecord.toJson());
print("Key: $key Type: ${key.runtimeType}");

The trace:
flutter: Key: 15 Type: int

@alextekartik
Copy link
Collaborator

Arg. It should even crash before as the return type of .add is String. Which flutter version are you using (and can you ensure that you have the lints and null safety enabled) ? Can you manage to reproduce in a unit test as I show above? (Sorry to ask but I cannot image how it can happen so far)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants