Home > Back-end >  Flutter error: The argument type 'Future<Object?> Function()' can't be assigned
Flutter error: The argument type 'Future<Object?> Function()' can't be assigned

Time:02-24

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?

CodePudding user response:

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

  • Related