tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
780 stars 64 forks source link

How to get a list of sembast records? #297

Closed MagnoGeek closed 8 months ago

MagnoGeek commented 2 years ago

Greetings, I am new to programming in dart, and with the use of sembast. With the following model I can create a list of patients in a store called patients_store:


class Pacientes extends Equatable {

  Pacientes({
     required this.items
  });

  List<Paciente> items;  

  factory Pacientes.fromJson(List<dynamic> data) {
    List<Paciente> items = data.map((i)=>Paciente.fromJson(i)).toList();    
    return new Pacientes(
      items: items
    );
  }

  Pacientes copyWith({
    List<Paciente>? items,
  }) {
    return Pacientes(
      items: items ?? this.items,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'items': items.map((x) => x.toMap()).toList(),
    };
  }

  factory Pacientes.fromMap(Map<dynamic, dynamic> map) {
    return Pacientes(
       items: List<Paciente>.from(map['items']?.map((x) => Paciente.fromMap(x))),
    );
  }

  String toJson() => json.encode(toMap());

  @override
  String toString() => 'Pacientes(items: $items)';

  @override
  List<Object> get props => [items];
}

Please, Be so nice and guide me, on how can I get that list created from sembast?

alextekartik commented 2 years ago

You should not store the whole Patientes.toMap but instead store each Patient as a record in your store

MagnoGeek commented 2 years ago

You should not store the whole Patientes.toMap but instead store each Patient as a record in your store

Thanks for your replay. I implemented in this way:

class PacienteRepositoryImpl extends PacienteRepository {
  final Database _db = GetIt.I.get();
  final StoreRef _store = intMapStoreFactory.store("paciente_store");

  @override
   Future<List> insertPacientes( Pacientes pacientes) async {

     final list = [];
     pacientes.items.forEach((patient) { 
       list.add( patient.toMap() );
     });
    return await _store.addAll( _db, list );

  }

Is this the way correct?

so I make the call:

"Patientes" have two records.

final pacientesId = await _pacienteRepository.insertPacientes( pacientes ); final pacientesStore = await _pacienteRepository.getPacientes();

when I do a print: print('Id pacientes creado Local $pacientesId');

it prints: Id pacientes creado Local [1, 2]

when I do a print: print('Pacientes Almacenados Localmente:'); print(pacientesStore); print('');

It prints: It just shows one record even when there're two

[Record(paciente_store, 1) 
{ 
   paciente_id: 108,
   tipo_identificacionId: 1, 
   tipo_identificacion: CC, 
   identificacion: 38957079,
    primer_nombre: ROSA, 
    primer_apellido: LOPEZ, 
    cie: [
         {
         codigo: R263, 
         nombre: INMOVILIDAD, 
         fecha_diagnostico: 2019-10-19, 
         status: 1}, 
         {
         codigo: F000, 
         nombre: DEMENCIA EN LA ENFERMEDAD DE ALZHEIMER, DE COMIENZO TEMPRANO (G30.0†),
         fecha_diagnostico: 2019-10-19,
          status: 1}
          ]
}

How Can I access this records my friend? thank you again for your answer...

alextekartik commented 2 years ago

Hard to help you here with more information (I cannot tell whether getPacientes is implemented correctly if I don't see it) Could you try to reproduce your issue in a unit test?

MagnoGeek commented 2 years ago

@alextekartik Thanks for your replay. I'm using freezed to create the models, How can I implement it to retrieve data from a store?