简短的问题。
我正在尝试学习如何使用对象以指定列表等并访问对象属性。我只是无法使其正常工作。
所以,这就是我要实现的目标:
我的代码:
对象类
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()语句,并且这样做根本不会打印任何内容。
有什么建议吗?
最诚挚的问候!
答案 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)
请参见下面的代码示例:
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
}