Flutter error: The argument type 'Future<Object?> Function()' can't be assigned to the parameter type 'FutureOr<dynamic> Function(dynamic)'

178

your then method always return a value, even if your future is void, so you need to change your code to:

(_) => Navigator.of(context).popAndPushNamed(HomeRoute)

more info about "then" here: https://api.flutter.dev/flutter/dart-async/Future/then.html

Share:
178
rawm
Author by

rawm

I'm learning and trying to make stuff that could help make the world better. Freedom and democracy are the current goals.

Updated on January 03, 2023

Comments

  • rawm
    rawm 10 months

    I'm getting this error when I put a Navigator in .then method of an async function called when the submit button is pressed.

        await profileNotifier(false)
            .createProfile(
                context: context,
                profileDTO: ProfileDTO(
                  useremail: userEmail,
                  profile_name: profileName,
                ))
            .then(
                () => Navigator.of(context).popAndPushNamed(HomeRoute));
    

    The future function is in another file.

      Future createProfile({
        required BuildContext context,
        required ProfileDTO profileDTO,
      }) async {
        try {
          await _profileAPI.createProfile(profileDTO);
        } catch (e) {
          print(e.toString());
        }
      }
    

    Can someone help me understand the error and how to rectify it. What changes do I need to make to get it to work?