Flutter: 'Map<String, dynamic> Function()' can't be assigned to 'Map<String, dynamic>'
The 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.
Fix it fast
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 analyze2Match what you see, then jump to its fix
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 analyzeConfirmed by pub.dev
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 analyzeConfirmed by dart.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
Reading a Firestore number into a Dart double throws a type error
TypeError
Firestore stores integers and floating-point numbers as separate types, and Dart keeps int and double apart too.
2 causes, each with a fixMobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fix