Home > OS >  Can someone assist me to update this functional programming error handling example for null safety i
Can someone assist me to update this functional programming error handling example for null safety i

Time:11-27

I'm learning proper error handling, I feel THIS is the best possible method of handling errors I've found. Problem is that the project is old as hell and not updated for null safety.

Project works well with old version. Tell me how I can get over this compile time error:

The argument type 'Either<Failure, dynamic>' can't be assigned to the parameter type 'Either<Failure, Post>'

This occurs due to labeling _post in PostChangeNotifier class as "late"...

import 'dart:convert';
import 'dart:io';
import 'package:dartz/dartz.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ProviderScope(
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.orange,
        ),
        home: const HomePage(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey,
      body: Flex(
        direction: Axis.horizontal,
        children: [Expanded(
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Consumer(
                  builder: (ctx, ref, child) {
                    if (ref.read(notifier).state == NotifierState.initial) {
                      return const Text('Press the button');
                    } else if (ref.read(notifier).state == NotifierState.loading) {
                      return const CircularProgressIndicator();
                    } else {
                      return ref.read(notifier).post.fold(
                            (failure) => Text(failure.toString()),
                            (post) => Text(post.toString()),
                          );
                    }
                  },
                ),
                Consumer(
                  builder: (ctx, ref,child) {
                    return ElevatedButton(
                    onPressed: () {
                      ref.read(notifier).getOnePost();
                    },
                    child: const Text('Get Post'));
                  },
                ),
              ]),
        ),]
      ),
    );
  }
}

class PostService {
  final httpClient = FakeHttpClient();
  Future<Post?> getOnePost() async {
    try {
      final responseBody = await httpClient.getResponseBody();
      return Post.fromJson(responseBody);
    } on SocketException {
      throw Failure('No Internet connection            
  • Related