2

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.

4
  • Can you add your fromJson method and show us the line where the error was given?
    – Benjamin
    May 30, 2021 at 3:05
  • Thank you for answering Benjamin. I added the Json and the line with the error to the initial question. May 30, 2021 at 3:13
  • Where do you do define your driver variable? And can you try printing out doc.data() and seeing what you get?
    – Benjamin
    May 30, 2021 at 3:15
  • I need to call the variable from my page controller like this in the users profile Container(child: Text(mapController.driver!.username, I cannot really print the doc.data() because that is the error i need help with and the code editor won't let me compile. However, I do know it contains the fields inside the document May 30, 2021 at 3:27

3 Answers 3

12

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.

3

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>);
1

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>>>.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.