Created
May 28, 2024 22:22
-
-
Save VB10/0712b769fc12913f2cf09fb2348f8d0d to your computer and use it in GitHub Desktop.
Error Handling from widget with exception manager
This file contains hidden or 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:flutter/foundation.dart'; | |
import 'package:flutter/material.dart'; | |
class User { | |
final String? name; | |
final int? age; | |
const User(this.name, this.age); | |
bool isEmpty() { | |
return name == null || age == null; | |
} | |
MapEntry<String, int> get nameAndAge { | |
if (isEmpty()) CustomException.instance.throwException('User not found'); | |
return MapEntry(name!, age!); | |
} | |
} | |
class HomeView extends StatefulWidget { | |
const HomeView({super.key}); | |
@override | |
State<HomeView> createState() => _HomeViewState(); | |
} | |
class _HomeViewState extends State<HomeView> with _HomeViewMixin { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('HomeView'), | |
), | |
body: _users == null | |
? const Center( | |
child: CircularProgressIndicator(), | |
) | |
: _UserListView(users: _users!), | |
); | |
} | |
} | |
class _UserListView extends StatelessWidget { | |
const _UserListView({required this.users}); | |
final List<User> users; | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: users.length, | |
itemBuilder: (context, index) { | |
return UserCard(user: users[index]); | |
}, | |
); | |
} | |
} | |
mixin _HomeViewMixin on State<HomeView> { | |
List<User>? _users; | |
Future<void> fetchUser() async { | |
final backendResponse = List<User?>.generate( | |
10, | |
(index) { | |
if (index.isOdd) return null; | |
return User('User $index', index); | |
}, | |
); | |
await Future.delayed(Durations.short1); | |
/// 1 | |
_users = backendResponse | |
.where((element) => element != null) | |
.cast<User>() | |
.toList(); | |
setState(() {}); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
fetchUser(); | |
} | |
} | |
class UserCard extends StatelessWidget { | |
const UserCard({super.key, required this.user}); | |
final User user; | |
/// yasi 30dan buyukse sol taraf yesil olacak kirmizi | |
/// title olarak adi gozukecek | |
/// age gozukecek | |
@override | |
Widget build(BuildContext context) { | |
final latestUserData = user.nameAndAge; | |
return ListTile( | |
title: Text(latestUserData.key), | |
subtitle: Text(latestUserData.value.toString()), | |
leading: Container( | |
width: 50, | |
height: 50, | |
color: latestUserData.value > 30 ? Colors.green : Colors.red, | |
), | |
); | |
} | |
} | |
abstract class CustomException { | |
void throwException(String message); | |
static CustomException get instance => | |
kDebugMode ? DevelopmentException() : ProductionException(); | |
} | |
class DevelopmentException implements CustomException { | |
@override | |
void throwException(String message) { | |
throw Exception(message); | |
} | |
} | |
class ProductionException implements CustomException { | |
/// custom end point | |
@override | |
void throwException(String message) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment