list_people.dart 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'dart:io';
  2. import 'dart_tutorial/addressbook.pb.dart';
  3. import 'dart_tutorial/addressbook.pbenum.dart';
  4. /// Iterates though all people in the AddressBook and prints info about them.
  5. void printAddressBook(AddressBook addressBook) {
  6. for (var person in addressBook.people) {
  7. print('Person ID: ${person.id}');
  8. print(' Name: ${person.name}');
  9. if (person.hasEmail()) {
  10. print(' E-mail address:${person.email}');
  11. }
  12. for (var phoneNumber in person.phones) {
  13. switch (phoneNumber.type) {
  14. case Person_PhoneType.MOBILE:
  15. print(' Mobile phone #: ');
  16. break;
  17. case Person_PhoneType.HOME:
  18. print(' Home phone #: ');
  19. break;
  20. case Person_PhoneType.WORK:
  21. print(' Work phone #: ');
  22. break;
  23. default:
  24. print(' Unknown phone #: ');
  25. break;
  26. }
  27. print(phoneNumber.number);
  28. }
  29. }
  30. }
  31. /// Reads the entire address book from a file and prints all
  32. /// the information inside.
  33. void main(List<String> arguments) {
  34. if (arguments.length != 1) {
  35. print('Usage: list_person ADDRESS_BOOK_FILE');
  36. exit(-1);
  37. }
  38. // Read the existing address book.
  39. final file = new File(arguments.first);
  40. final addressBook = new AddressBook.fromBuffer(file.readAsBytesSync());
  41. printAddressBook(addressBook);
  42. }