The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'

Viewed 2759

I am trying to retrieve data from a document in Firestore using a Widget. I used this in the past, I guess with a different version of Dart or flutter, and it worked. Now this is showing the following error when I use (doc.data()) The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'

Here's the code

DriverProvider driverProvider;

void getDriverInfo() {
    Stream<DocumentSnapshot> driverStream =
        driverProvider!.getbyIdStream(FirebaseAuth.instance.currentUser!.uid);
    driverStream.listen((DocumentSnapshot doc) {
      driver = Driver.fromJson(doc.data());
    });
  }

Here's where driverProvider! comes from

import 'package:brokerdrivers/models/user-models.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class DriverProvider {
  CollectionReference? _ref;

  DriverProvider() {
    _ref = FirebaseFirestore.instance.collection('Drivers');
  }

  Future? create(Driver driver) {
    String? errorMessage;

    try {
      return _ref!.doc(driver.id).set(driver.toJson());
    } catch (e) {
      print(e);
      errorMessage = e.toString();
      return Future.error(errorMessage);
    }
  }

  Stream<DocumentSnapshot> getbyIdStream(String id) {
    return _ref!.doc(id).snapshots(includeMetadataChanges: true);
  }
}



and this is where getbyIdStream comes from


  Stream<DocumentSnapshot> getbyIdStream(String id) {
    return _ref!.doc(id).snapshots(includeMetadataChanges: true);
  } 

This is my Json model

import 'dart:convert';

Driver driverFromJson(String str) => Driver.fromJson(json.decode(str));

String driverToJson(Driver data) => json.encode(data.toJson());

class Driver {
  Driver({
    required this.id,
    required this.username,
    required this.email,
    required this.truck,
    required this.carrier,
    required this.insurance,
  });

  String id;
  String username;
  String email;
  String truck;
  String carrier;
  String insurance;

  factory Driver.fromJson(Map<String, dynamic> json) => Driver(
        id: json["id"],
        username: json["username"],
        email: json["email"],
        truck: json["truck"],
        carrier: json["carrier"],
        insurance: json["insurance"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "username": username,
        "email": email,
        "truck": truck,
        "carrier": carrier,
        "insurance": insurance,
      };
}


and this is the line with the error

driver = Driver.fromJson(doc.data());

Thanks all for the help.

3 Answers

This error is a result of the new changes to the cloud_firestore API. You need to specify the type of data you're getting from your DocumentSnapshot

Update this line:

    driver = Driver.fromJson(doc.data());

to this:

    driver = Driver.fromJson(doc.data() as Map<String, dynamic>);

Check out the guide for migration to cloud_firestore 2.0.0.

In addition to Victor's answer, with new null safety, you would also need to check for null or (less recommended) convert a non-nullable (if you're sure the values can never be null):

driver = Driver.fromJson(doc.data()! as Map<String, dynamic>);

In addition to Victor's answer, you can also do this:

Stream<DocumentSnapshot<Map<String, dynamic>> driverStream =
    driverProvider!.getbyIdStream(FirebaseAuth.instance.currentUser!.uid);
driverStream.listen((DocumentSnapshot doc) {
  driver = Driver.fromJson(doc.data());
});

This makes the data() method return a Map<String, dynamic> so you don't have to cast it. Though you might have to ensure it's not null. You would also have to modify your getIdByStream method to return Stream<DocumentSnapshot<Map<String, dynamic>>>.

Related