Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter: 'Map<String, dynamic> Function()' can't be assigned to 'Map<String, dynamic>'

ErrorThe argument type 'Map<String, dynamic> Function()' can't be assigned to the parameter type 'Map<String, dynamic>'

Short answer

In cloud_firestore, DocumentSnapshot.data is a method. Passing doc.data without parentheses passes the function itself, so Dart says a function cannot be a Map. Call it as doc.data(). It returns a nullable value, so check for null or cast it before giving it to a constructor.

  • Applies to Dart 3.9, Flutter 3.35, current cloud_firestore
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the argument that the red line points to. A name with no parentheses after it, such as doc.data, is the function itself.

    grep -rn "\.data[^(a-zA-Z]" lib/
    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1The message names 'Map<String, dynamic> Function()' as the argument type.data is used without parenthesesFix
    2. 2After adding (), the error becomes about 'Map<String, dynamic>?' or 'Object?'.data() can be null, or has another type than your parameterFix

What does the error look like?

  • The argument type 'Map<String, dynamic> Function()' can't be assigned to the parameter type 'Map<String, dynamic>'
  • argument_type_not_assignable

Cause 1 of 2

data is used without parentheses

1Check

In cloud_firestore the signature is T? data(), so doc.data is a function and doc.data() is the contents.

grep -rn "fromJson(.*\.data\b" lib/

2Fix

Add the parentheses.

// before
final item = Item.fromJson(doc.data);

// after
final item = Item.fromJson(doc.data()!);

3Verify

Run the analyzer.

dart analyze

Confirmed by pub.dev

Did this fix it?

Cause 2 of 2

data() can be null, or has another type than your parameter

1Check

data() returns a nullable T, so the value can be null, and its static type is whatever T the snapshot was created with.

grep -rn "data()" lib/

2Fix

Handle null with a test, or cast when you are sure of the type. The Dart docs list a type test with a fallback and an explicit cast as the two fixes.

final data = doc.data();
if (data is Map<String, dynamic>) {
  return Item.fromJson(data);
}
throw StateError('Document ${doc.id} is empty');

3Verify

The analyzer shows no error and the screen loads a known document.

dart analyze

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Handle the null case of data() in one helper so every screen does it the same way.
  • Run flutter analyze in CI so the error never reaches a build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.