使用对象列表,如何访问对象属性并打印它们?

时间:2019-07-30 01:18:40

标签: java android flutter dart

简短的问题。

我正在尝试学习如何使用对象以指定列表等并访问对象属性。我只是无法使其正常工作。

所以,这就是我要实现的目标:

  1. 创建列表objList;
  2. 将对象添加到列表中
  3. 从列表中获取特定对象
  4. 在控制台中打印!

我的代码:

对象类

class SearchObject {
  final String barName;
  final String latitudeDbRef;

  SearchObject({this.barName, this.latitudeDbRef});
}

列表声明和对象

List<SearchObject> searchObject = new List();
SearchObject sbo = new SearchObject();

将对象添加到列表

searchObject.add(SearchObject(barName: value, latitudeDbRef: "test"));

访问并打印特定对象

print(searchObject.getRange(index, index + 1).map((sbo) {
                    String bar = sbo.barName.toString();
                    String lat = sbo.latitudeDbRef.toString();
                    print("Barname: $bar");
                    print("latName: $lat");
                  }));

输出

flutter: Barname: barname
flutter: latName: test
flutter: (null)   <- Get rid of this?

注意: 我已经尝试过删除searchObject.getRange()代码周围的print()语句,并且这样做根本不会打印任何内容。

有什么建议吗?

最诚挚的问候!

2 个答案:

答案 0 :(得分:0)

class SearchObject {
  final String barName;
  final String latitudeDbRef;

  const SearchObject({this.barName, this.latitudeDbRef});
}

void main() {
  final newSearchObject = new SearchObject(barName: "foo", latitudeDbRef: "bar");

  // How to populate a list:

  // Method 0: inline 
  final searchObjects = <SearchObject>[newSearchObject];

  // Method 1: Using .add
  final searchObjects2 = <SearchObject>[];
  searchObjects2.add(newSearchObject);

  // Method 2: Using the + operator
  final searchObjects3 = searchObjects + [newSearchObject];

  // Method 3: Using the spread operator:
  final searchObjects4 = [...searchObjects, newSearchObject];

  // Accessing a specific object using the [] operator
  final specificObject = searchObjects4[0]; // Be careful, It will throw if there's no item for that index in the list.

  print("barName: ${specificObject.barName}"); // prints: barName: foo
  print("latitudeDbRef: ${specificObject.latitudeDbRef}"); // prints: latitudeDbRef: bar

  // Iterate over a list to print the details:
  searchObjects4.forEach((object) {
    print("barName: ${object.barName}"); // prints: barName: foo
    print("latitudeDbRef: ${object.latitudeDbRef}"); // prints: latitudeDbRef: bar
  });

  // Collect the details themselves inside an iterable:
  final detailsIterable = searchObjects4.map((object) {
    return "barName: ${object.barName}, latitudeDbRef: ${object.latitudeDbRef}";
  });

  final detailsList = detailsIterable.toList();

  print(detailsList); // prints: [barName: foo, latitudeDbRef: bar, barName: foo, latitudeDbRef: bar]
}

我希望这会有所帮助。

答案 1 :(得分:0)

在对象列表中,您可以使用list.singleWhere

使用其属性访问对象。

请参见下面的代码示例:

class SearchObject {
  final String barName;
  final String latitudeDbRef;

  SearchObject({this.barName, this.latitudeDbRef});
}

void main(){
  List<SearchObject> sbList = new List();

  sbList.add(SearchObject(barName: 'barName0', latitudeDbRef: "latitudeDbRef0"));
  sbList.add(SearchObject(barName: 'barName1', latitudeDbRef: "latitudeDbRef1"));
  sbList.add(SearchObject(barName: 'barName2', latitudeDbRef: "latitudeDbRef2"));
  sbList.add(SearchObject(barName: 'barName3', latitudeDbRef: "latitudeDbRef3"));

  // retriving searchObject using its properties from searchObjectList.
  SearchObject retrievedSB = sbList.singleWhere((tempSB) => tempSB.barName=="barName2" && tempSB.latitudeDbRef=="latitudeDbRef2");

  print(retrievedSB.barName + " " + retrievedSB.latitudeDbRef);
  // output: barName2 latitudeDbRef2


  // retriving searchObject using ONE of its properties from searchObjectList.
  SearchObject retrievedSB2 = sbList.singleWhere((tempSB) => tempSB.latitudeDbRef=="latitudeDbRef1");

  print(retrievedSB2.barName + " " + retrievedSB2.latitudeDbRef);
  //output: barName1 latitudeDbRef1
}