Lab 3
Lab 3
Lab 3
collections]
Task 1:
Output:
Future<String> changeUsername () async {
try {
return await fetchNewUsername();
} catch (err) {
return err.toString();
}
}
Task 2:
Part 1:
import 'dart:convert';
import 'dart:isolate';
import 'package:http/http.dart' as http;
class SearchResult {
// TODO: add properties and constructors to initialise your model
class
String content_type;
String title;
List<String> tags;
String pubDate;
String url;
SearchResult(
this.content_type, this.title, this.tags, this.pubDate,
this.url);
@override
String toString() {
// TODO: implement toString
return "Search Result => 'ContentType' : ${content_type}
'title' : ${title} 'tags' : ${tags} 'PubDate': ${pubDate} 'url' : $
{url}";
}
}
class APIClient {
Future<List<SearchResult>> downloadAndParseJson() async {
const uriString =
'https://codewithandrea.com/search/search.json';
final response = await http.get(Uri.parse(uriString));
if (response.statusCode == 200) {
final parser = SearchResultsParser(response.body);
return parser.parseInBackground();
} else {
throw Exception('Failed to load json');
}
}
}
class SearchResultsParser {
SearchResultsParser(this.encodedJson);
final String encodedJson;
Future<List<SearchResult>> parseInBackground() async {
final p = ReceivePort();
await Isolate.spawn(_decodeAndParseJson, p.sendPort);
Output:
Part 2:
import 'dart:convert';
import 'dart:isolate';
import 'package:http/http.dart' as http;
class SearchResult {
// TODO: add properties and constructors to initialise your model
class
String content_type;
String title;
List<String> tags;
String pubDate;
String url;
SearchResult(
this.content_type, this.title, this.tags, this.pubDate,
this.url);
@override
String toString() {
// TODO: implement toString
return "Search Result => 'ContentType' : ${content_type}
'title' : ${title} 'tags' : ${tags} 'PubDate': ${pubDate} 'url' : $
{url}";
}
}
class SearchResultCustom {
// TODO: add properties and constructors to initialise your model
class
int albumId;
int id;
String title;
String thumbnailUrl;
String url;
SearchResultCustom(
this.albumId, this.title, this.thumbnailUrl, this.id,
this.url);
@override
String toString() {
// TODO: implement toString
return "Search Result => 'Album Id' : ${albumId} 'ID' : ${id}
'Thumbnail URL' : ${thumbnailUrl} 'ID': ${id} 'url' : ${url}";
}
}
class APIClient {
Future<List<dynamic>> downloadAndParseJson(var url) async {
var uriString = url;
final response = await http.get(Uri.parse(uriString));
if (response.statusCode == 200) {
final parser = SearchResultsParser(response.body);
return parser.parseInBackground();
} else {
throw Exception('Failed to load json');
}
}
}
class SearchResultsParser {
SearchResultsParser(this.encodedJson);
final String encodedJson;
Future<List<dynamic>> parseInBackground() async {
final p = ReceivePort();
await Isolate.spawn(_decodeAndParseJson, p.sendPort);
return await p.first;
}
Output:
Task 3:
class EmailAddress {
String _email;
EmailAddress(this._email);
@override
String toString() {
return _email;
}
}
Iterable<EmailAddress> parseEmailAddress(
Iterable<String> listOfEmailAddresses) {
return listOfEmailAddresses.map((s_email) =>
EmailAddress(s_email));
}
bool anyInvalidEmailAddress(Iterable<EmailAddress>
listOfEmailAddresses) {
if (listOfEmailAddresses
.any((item) => !isValidEmailAddressRegExp(item.getEmail))) {
return true;
} else {
return false;
}
}
Iterable<EmailAddress> validEmailAddresses(
Iterable<EmailAddress> listOfEmailAddresses) {
return listOfEmailAddresses
.where((item) => isValidEmailAddressRegExp(item.getEmail));
}
void main() {
var mails = {"umer@gmail.com", "umer", "umer@",
"umer.farooq@gmail.com"};
Iterable<EmailAddress> addrs = parseEmailAddress(mails);
print("Iterable of string Emails: ${mails}");
print("Iterable of Email Addresses: ${addrs}");
print("Valid Email Addresses: ${validEmailAddresses(addrs)}");
print(
"Invalid Email Addresses:(check for existence) $
{anyInvalidEmailAddress(addrs)}");
}
output: