Emir Kutlugün
Emir Kutlugün

Reputation: 447

Error "The argument type 'Object' can't be assigned to the parameter type 'String'" while reading a Firestore collection

I was trying to get data from firebaseFirestore, but it shows error below, also this article didn't help https://firebase.flutter.dev/docs/firestore/usage/#typing-collectionreference-and-documentreference.

Code

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:greatr/models/ChatRoom.dart';

FirebaseFirestore firestore = FirebaseFirestore.instance;

    Future getChatRooms() async {
      List<ChatRoom> rooms = [];
      QuerySnapshot response = await firestore.collection('chat_rooms').get();
      response.docs.forEach((element) {
        rooms.add(ChatRoom.fromJson(element.data()!));
      });
     
    }

Error

The argument type 'Object' can't be assigned to the parameter type 'String'.

Upvotes: 0

Views: 89

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

You don't share the code of the ChatRoom class, but I make the assumption it is similar to the example with the Movie class in the documentation you refer to in your question.

If this assumption is correct, the following should do the trick:

final chatRoomsRef = FirebaseFirestore.instance.collection('chat_rooms').withConverter<ChatRoom>(
      fromFirestore: (snapshot, _) => ChatRoom.fromJson(snapshot.data()!),
      toFirestore: (chat_room, _) => chat_room.toJson(),
    );

//...


Future getChatRooms() async {

  List<QueryDocumentSnapshot<ChatRoom>> chat_rooms = await chatRoomsRef
      .get()
      .then((snapshot) => snapshot.docs);     
   //...

}

Upvotes: 1

Related Questions