"Fossies" - the Fresh Open Source Software Archive

Member "protobuf-3.21.1/examples/add_person.dart" (27 May 2022, 1829 Bytes) of package /linux/misc/protobuf-all-3.21.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import 'dart:io';
    2 
    3 import 'dart_tutorial/addressbook.pb.dart';
    4 
    5 /// This function fills in a Person message based on user input.
    6 Person promptForAddress() {
    7   final person = Person();
    8 
    9   print('Enter person ID: ');
   10   final input = stdin.readLineSync();
   11   person.id = int.parse(input);
   12 
   13   print('Enter name');
   14   person.name = stdin.readLineSync();
   15 
   16   print('Enter email address (blank for none) : ');
   17   final email = stdin.readLineSync();
   18   if (email.isNotEmpty) person.email = email;
   19 
   20   while (true) {
   21     print('Enter a phone number (or leave blank to finish): ');
   22     final number = stdin.readLineSync();
   23     if (number.isEmpty) break;
   24 
   25     final phoneNumber = Person_PhoneNumber()..number = number;
   26 
   27     print('Is this a mobile, home, or work phone? ');
   28 
   29     final type = stdin.readLineSync();
   30     switch (type) {
   31       case 'mobile':
   32         phoneNumber.type = Person_PhoneType.MOBILE;
   33         break;
   34       case 'home':
   35         phoneNumber.type = Person_PhoneType.HOME;
   36         break;
   37       case 'work':
   38         phoneNumber.type = Person_PhoneType.WORK;
   39         break;
   40       default:
   41         print('Unknown phone type.  Using default.');
   42     }
   43     person.phones.add(phoneNumber);
   44   }
   45 
   46   return person;
   47 }
   48 
   49 /// Reads the entire address book from a file, adds one person based
   50 /// on user input, then writes it back out to the same file.
   51 void main(List<String> arguments) {
   52   if (arguments.length != 1) {
   53     print('Usage: add_person ADDRESS_BOOK_FILE');
   54     exit(-1);
   55   }
   56 
   57   final file = File(arguments.first);
   58   AddressBook addressBook;
   59   if (!file.existsSync()) {
   60     print('File not found. Creating new file.');
   61     addressBook = AddressBook();
   62   } else {
   63     addressBook = AddressBook.fromBuffer(file.readAsBytesSync());
   64   }
   65   addressBook.people.add(promptForAddress());
   66   file.writeAsBytes(addressBook.writeToBuffer());
   67 }