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