2

I want to print specific individualized object properties with this extension- Source: HERE

extension ExtendedIterable<E> on Iterable<E> {
  /// Like Iterable<T>.map but callback have index as second argument
  Iterable<T> mapIndex<T>(T f(E e, int i)) {
    var i = 0;
    return this.map((e) => f(e, i++));
  }

  void forEachIndex(void f(E e, int i)) {
    var i = 0;
    this.forEach((e) => f(e, i++));
  }
}

I am saving user data from textFields into a Hive box.

When I do the following...

final box = Hive.box(personTable).values.toList();
    final hiveBox = Hive.box(personTable);
    final indexingBox = box.mapIndex((e, i) => 'item$e index$i');
    final Person person = hiveBox.getAt(0);
    print(person);
    print(indexingBox);

I get the following printed:

flutter: {John, Biggs, 34, Active}
flutter: (item{John, Biggs, 34, Active} index0, item{Kostas, Panger, 76, Active} index1, item{Ben, Kenobi, 78, Deactivated} index2, ..., item{Luke, Skywalker, 45, Active} index5, item{Darth, Vader, 54, Active} index6)

I want to be able to enumerate selectively, each object property as I please.

This is what I want to be able to print:

  • flutter: John. // index 0 firstName
  • flutter: Kostas // index 1 firstName
  • flutter: Vader // index 6 lastname

Class saving into Hive box:

import 'package:hive/hive.dart';
part 'person.g.dart';

@HiveType(typeId: 0)
class Person {
  @HiveField(0)
  final String firstName;
  @HiveField(1)
  final String lastName;
  @HiveField(2)
  final String age;
  @HiveField(3)
  final String status;
  Income({
    this.firstName,
    this.lastName,
    this.age,
    this.status,
  });
  @override
  String toString() {
    return '{${this.firstName}, ${this.lastName}, ${this.age}, ${this.status}}';
  }
}

If I can't solve this once and for all my head may as well explode, this is part of a bigger picture of making a DataTable very simple and dynamically loading. Help is appreciated!

RobbB
  • 1,214
  • 11
  • 39

2 Answers2

1

From your list, you can just get the firstName and pass it in the function :

If you use a model :

class Person {
  final String firstName;
  final String lastName;
  final String age;
  final String status;

  Person(
    this.firstName,
    this.lastName,
    this.age,
    this.status,
  );
  @override
  String toString() {
    return '{${this.firstName}, ${this.lastName}, ${this.age}, ${this.status}}';
  }

  factory Person.fromJson(List<String> list) {
    return Person(list[0], list[1], list[2], list[3]);
  }
}
    
    
void main() {

List<String> listPerson = <String>[];
  
  final box = Hive.box(personTable).values.toList();
  
  box.forEach((element) { final test = element.toString(); listPerson.add(test); print(test); });

List<String> listPersonWithoutBracket = <String>[];

  for (var i = 0; i < listPerson.length; i++) {
    String strWithoutBracket =
        listPerson[i].replaceAll("{", "").replaceAll("}", "").trim();

    listPersonWithoutBracket.add(strWithoutBracket);
  }

  List<List<String>> listPersonResult = <List<String>>[];

  for (var i = 0; i < listPersonWithoutBracket.length; i++) {
    var strSplit = listPersonWithoutBracket[i].split(", ");
    listPersonResult.add(strSplit);
  }
  
  List<Person> listPersonFinal = listPersonResult.map((item) => Person.fromJson(item)).toList();
  
  final allFirstName = listPersonFinal.map((item) {
          return item.firstName;
      });
  
  final indexingBox = allFirstName.mapIndex((e, i) => '$e');
      print(indexingBox);
        
    }

Outputs

 John, Kostas
Lapa Ny Aina Tanjona
  • 1,138
  • 1
  • 9
  • 19
  • Tried this and I get an error: `The following NoSuchMethodError was thrown building PersonPage(dirty): Class 'Person' has no instance method '[]'. Receiver: Instance of 'Person' Tried calling: [](0)` – RobbB Mar 05 '21 at 05:21
  • Sorry I somehow missed the top half of you're post. Will try that now! – RobbB Mar 05 '21 at 05:23
  • You have named your class in your constructor Income instead of Person! Person({ this.firstName, this.lastName, this.age, this.status, }); – Lapa Ny Aina Tanjona Mar 05 '21 at 05:24
  • I posted that error from a second section of my project where I tried it, I updated it with the Person section error – RobbB Mar 05 '21 at 05:26
  • Can you output the box variable ? – Lapa Ny Aina Tanjona Mar 05 '21 at 05:29
  • The box variable prints like so: `flutter: (item{John, Biggs, 34, Active} index0, item{Kostas, Panger, 76, Active} index1, item{Ben, Kenobi, 78, Deactivated} index2, ..., item{Luke, Skywalker, 45, Active} index5, item{Darth, Vader, 54, Active} index6). Anyway trying this again – RobbB Mar 05 '21 at 05:39
  • Okay tried it and I get this error: `type 'Person' is not a subtype of type 'Map'` which I do not understand – RobbB Mar 05 '21 at 05:51
  • 1
    Yes! This error appears because the type of the variable in my example is not the same with the type of the variable in hivebox! Can you tell me what is the type of the box variable? String or List? And can you output the hiveBox variable too? Thanks. I've never work with Hive before but if I know the correct variable, we resolve your question. – Lapa Ny Aina Tanjona Mar 05 '21 at 05:55
  • Absolutely, the box is List and the hiveBox is Box – RobbB Mar 05 '21 at 05:57
  • So far I have gotten closer using forEach() like so: `box.forEach((element) { final test = element.toString(); print(test); });` this prints enumerations for each object stored in the box Example: `flutter: {John, Biggs, 34, Active} flutter: {Kostas, Panger, 76, Active}` – RobbB Mar 05 '21 at 06:32
  • Okay! I can take your solution from there. I was creating an app and really testing locally with the hive package. – Lapa Ny Aina Tanjona Mar 05 '21 at 06:44
  • Unfortunately the change gave me the exact same results as the forEach example. Prints each object individually but cannot access each object property. This is crazy! I feel like this is a Hive problem or hive complexity and Hive documentation isn't thorough enough for performing actions like this. – RobbB Mar 05 '21 at 07:52
  • 1
    Yes, but with my example you can get the List with a Person Model : ( List listPersonFinal ) – Lapa Ny Aina Tanjona Mar 05 '21 at 08:10
  • I didn’t notice that, I will give it another shot a little later today. Thanks for the help so far! – RobbB Mar 05 '21 at 14:46
0

SOLVED, FINALLY!!!

box.forEachIndex((e, i) {
      final hiveBox = Hive.box(personTable);
      final person = hiveBox.getAt(i) as Person;
      print('${person.firstName}');
    });

Console

flutter: John
flutter: Obi-wan
flutter: Luke
RobbB
  • 1,214
  • 11
  • 39