Created
October 8, 2023 22:40
-
-
Save iamEtornam/c206093fd72ce6c7d6730ba6aaa91df5 to your computer and use it in GitHub Desktop.
This code sample uses Records or Custom Result class to pass data and exceptions to the UI.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:example_user_app/result.dart'; | |
import 'package:example_user_app/user.dart'; | |
abstract class Core { | |
({User? user, Exception? ex}) getUser(); | |
Result getFilterNames({required String name}); | |
} | |
class CoreImpl implements Core { | |
User? user; | |
///using records approach | |
@override | |
({User? user, Exception? ex}) getUser() { | |
user = User(firstName: 'Kofi', lastName: 'Mensah',age: 25); ///Add age: 25 to bypass error | |
//no age added so throw error | |
if (user?.age == null) { | |
final ex = Exception('User age not present'); | |
return (user: null, ex: ex); | |
} | |
user?.age = 22; | |
return (user: user, ex: null); | |
} | |
///using custom class approach | |
@override | |
Result getFilterNames({required String name}) { | |
Result res = Result(); | |
final users = ['Ama', 'Kofi']; | |
final results = users | |
.where((names) => names.toLowerCase().contains(name.toLowerCase())) | |
.toList(); | |
if (results.isNotEmpty) { | |
res.result = results; | |
} else if (results.isEmpty) { | |
res.exception = Exception('No user found'); | |
} else { | |
res.exception = Exception('Something went wrong!'); | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:example_user_app/core.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData(primarySwatch: Colors.purple, useMaterial3: true), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final core = CoreImpl(); | |
String results = ''; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Spacer(), | |
TextFormField( | |
decoration: | |
const InputDecoration(hintText: 'Search for name here'), | |
onChanged: (value) { | |
if (value.isNotEmpty && value.length >= 3) { | |
searchNames(value); | |
} else { | |
setState(() { | |
results = ''; | |
}); | |
} | |
}, | |
), | |
const SizedBox(height: 10), | |
Text( | |
results, | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
const Spacer(), | |
], | |
), | |
), | |
), | |
floatingActionButton: FloatingActionButton.extended( | |
onPressed: () { | |
final res = core.getUser(); | |
if (res.ex != null) { | |
ScaffoldMessenger.of(context) | |
.showSnackBar(SnackBar(content: Text(res.ex!.toString()))); | |
} | |
if (res.user != null) { | |
setState(() { | |
results = res.user!.toString(); | |
}); | |
} | |
}, | |
tooltip: 'Get user', | |
label: const Text('Get user'), | |
icon: const Icon(CupertinoIcons.person_2_alt), | |
), | |
); | |
} | |
void searchNames(String name) { | |
final res = core.getFilterNames(name: name); | |
if (res.exception != null) { | |
ScaffoldMessenger.of(context) | |
.showSnackBar(SnackBar(content: Text(res.exception!.toString()))); | |
} | |
if (res.result != null) { | |
final names = res.result as List<String>; | |
setState(() { | |
results = names.join(',\n'); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///custom result class | |
class Result<T> { | |
T? result; | |
Exception? exception; | |
Result({this.result, this.exception}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///user model | |
class User { | |
String? firstName; | |
String? lastName; | |
int? age; | |
User({this.firstName, this.lastName, this.age}); | |
User.fromJson(Map<String, dynamic> json) { | |
firstName = json['first_name']; | |
lastName = json['last_name']; | |
age = json['age']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = {}; | |
data['first_name'] = firstName; | |
data['last_name'] = lastName; | |
data['age'] = age; | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment