rawm
rawm

Reputation: 299

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

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?

Upvotes: 2

Views: 1547

Answers (1)

Carlos Sandoval
Carlos Sandoval

Reputation: 867

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

Upvotes: 2

Related Questions