2

我想用这个扩展打印特定的个性化对象属性——来源:这里

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++));
  }
}

我正在将 textFields 中的用户数据保存到 Hive 框中。

当我执行以下操作时...

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);

我得到以下打印:

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)

我希望能够随意枚举每个对象属性。

这就是我希望能够打印的内容:

  • 扑扑:约翰。// 索引 0 名字
  • flutter: Kostas // 索引 1 firstName
  • flutter: Vader // 索引 6 姓氏

类保存到 Hive 框中:

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}}';
  }
}

如果我不能一劳永逸地解决这个问题,我的头脑可能会爆炸,这是使 DataTable 变得非常简单和动态加载的更大图景的一部分。帮助表示赞赏!

4

2 回答 2

1

从您的列表中,您可以获取 firstName 并将其传递给函数:

如果您使用模型:

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
于 2021-03-05T05:08:58.493 回答
0

终于解决了!!!

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

安慰

flutter: John
flutter: Obi-wan
flutter: Luke
于 2021-03-06T02:01:33.557 回答