Skip to content

Instantly share code, notes, and snippets.

@VB10
Created May 28, 2024 22:22

Revisions

  1. VB10 created this gist May 28, 2024.
    127 changes: 127 additions & 0 deletions error_manage_widget_with_exception.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    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) {}
    }