How can I output my object properties with this enumerating extension in Flutter/Dart?

986

Solution 1

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

Solution 2

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
Share:
986
RobbB
Author by

RobbB

Updated on December 28, 2022

Comments

  • RobbB
    RobbB over 1 year

    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
    RobbB about 3 years
    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
    RobbB about 3 years
    Sorry I somehow missed the top half of you're post. Will try that now!
  • Lapa Ny Aina Tanjona
    Lapa Ny Aina Tanjona about 3 years
    You have named your class in your constructor Income instead of Person! Person({ this.firstName, this.lastName, this.age, this.status, });
  • RobbB
    RobbB about 3 years
    I posted that error from a second section of my project where I tried it, I updated it with the Person section error
  • Lapa Ny Aina Tanjona
    Lapa Ny Aina Tanjona about 3 years
    Can you output the box variable ?
  • RobbB
    RobbB about 3 years
    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
    RobbB about 3 years
    Okay tried it and I get this error: type 'Person' is not a subtype of type 'Map<String, dynamic>' which I do not understand
  • Lapa Ny Aina Tanjona
    Lapa Ny Aina Tanjona about 3 years
    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.
  • RobbB
    RobbB about 3 years
    Absolutely, the box is List<dynamic> and the hiveBox is Box<dynamic>
  • RobbB
    RobbB about 3 years
    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}
  • Lapa Ny Aina Tanjona
    Lapa Ny Aina Tanjona about 3 years
    Okay! I can take your solution from there. I was creating an app and really testing locally with the hive package.
  • RobbB
    RobbB about 3 years
    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.
  • Lapa Ny Aina Tanjona
    Lapa Ny Aina Tanjona about 3 years
    Yes, but with my example you can get the List<Person> with a Person Model : ( List<Person> listPersonFinal )
  • RobbB
    RobbB about 3 years
    I didn’t notice that, I will give it another shot a little later today. Thanks for the help so far!