skip to Main Content

my error said:

Exception has occurred.
_TypeError (type ‘(ProfileDataset) => SelfProfilePage’ is not a subtype of type ‘(dynamic) => Widget’)

my codes:

import 'package:flutter/material.dart';

class AwaitJumpPage<T> extends StatefulWidget {
  const AwaitJumpPage({
    super.key,
    required this.future,
    required this.callback,
  });
  final Future<T> future;
  final Widget Function(T) callback;

  @override
  State<AwaitJumpPage> createState() => _AwaitJumpPageState<T>();
}

class _AwaitJumpPageState<T> extends State<AwaitJumpPage> {
  @override
  void initState() {
    widget.future.then((value) {
      if (mounted) {
        Navigator.pushReplacement(context, MaterialPageRoute(
          builder: (context) {
            // ignore: unnecessary_cast
            final fun = widget.callback as Widget Function(dynamic);
            return fun(value);
          },
        ));
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

How I using this class:

Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) {
          return AwaitJumpPage(
            future: [[a future that return ProfileDataset]],
            callback: (data){
              return SelfProfilePage(data);
            }
          );
        },
      ),
    );

My class SelfProfilePage is a widget really.

Can you help me, please?

2

Answers


  1. As I understand your final goal is crate something like that:

    import 'dart:math';
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(
        const MaterialApp(
          home: MyApp(),
        ),
      );
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ElevatedButton(
              onPressed: () => Navigator.of(context).push(
                MaterialPageRoute(
                  builder: (context) => AwaitJumpPage(
                    builderWithPayload: (context, payload) =>
                        SecondPage(payload: payload),
                    future: generateRandomNumber(),
                  ),
                ),
              ),
              child: const Text('Click me'),
            ),
          ),
        );
      }
    
      Future<int> generateRandomNumber() async {
        await Future.delayed(const Duration(seconds: 1));
        return Random().nextInt(100);
      }
    }
    
    class SecondPage<T> extends StatelessWidget {
      const SecondPage({
        super.key,
        required this.payload,
      });
    
      final T payload;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('SecondPage result: $payload'),
                ElevatedButton(
                  onPressed: () => Navigator.of(context).pop(),
                  child: const Text('Back'),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    typedef WidgetBuilderWithPayload<T> = Widget Function(
      BuildContext context,
      T payload,
    );
    
    class AwaitJumpPage<T> extends StatefulWidget {
      const AwaitJumpPage({
        super.key,
        required this.future,
        required this.builderWithPayload,
      });
      final Future<T> future;
      final WidgetBuilderWithPayload<T> builderWithPayload;
    
      @override
      State<AwaitJumpPage> createState() => _AwaitJumpPageState<T>();
    }
    
    class _AwaitJumpPageState<T> extends State<AwaitJumpPage> {
      @override
      void initState() {
        WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
    
        super.initState();
      }
    
      void _afterLayout(_) async {
        T result = await widget.future;
        if (!mounted) return;
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => SecondPage<T>(payload: result),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return const Scaffold(
          body: Center(
            child: CircularProgressIndicator(),
          ),
        );
      }
    }
    

    enter image description here

    Login or Signup to reply.
  2. Pass generic type ProfileDataset while calling AwaitJumpPage

    Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) {
              return AwaitJumpPage<ProfileDataset>(
                future: [[a future that return ProfileDataset]],
                callback: (data){
                  return SelfProfilePage(data);
                }
              );
            },
          ),
        );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search