Home > Software engineering >  The argument type 'Stream<DocumentSnapshot<Map<String, dynamic>>>' can&#
The argument type 'Stream<DocumentSnapshot<Map<String, dynamic>>>' can&#

Time:08-10

I have this error

"The argument type 'Stream<DocumentSnapshot<Map<String, dynamic>>>' can't be assigned to the parameter type 'Stream<DocumentSnapshot>?'."

Below, you will find the model for my data. I am trying to use this. I do not understand why the stream does not accept my data model TaskItems. I do not know how to fix the problem here. Please, can you help me? Thank you

class TaskItems {

  String? taskName;
  String status;
  String projectName;
  String parentProjectID;
  List <String>context;
 

  TaskItems(
    this.taskName,
    this.status,
    this.projectName,
    this.parentProjectID,
    this.context,
    
    );
}
@override
  Widget build(BuildContext context) {
    return
       StreamBuilder<DocumentSnapshot<TaskItems>>(
          stream: FirebaseFirestore.instance
              .collection('Users')
              .doc(FirebaseAuth.instance.currentUser!.uid)
              .collection('allTasks')
              .doc(widget.taskId)
              .snapshots(),

CodePudding user response:

.snapshots(), has a return type of Stream<DocumentSnapshot<Map<String, dynamic>>> whcih means your streambuilder can only subscribe to a Stream<DocumentSnapshot<Map<String, dynamic>>> stream.

You have to convert Stream<DocumentSnapshot<Map<String, dynamic>>> to List<TaskItems> or TaskItems

Assuming you are displaying the data in a listview after fetching here is how you woukd do it .

First edit your TaskItems class by adding toJson and from Json

class TaskItems {
  String? taskName;
  String? status;
  String? projectName;
  String? parentProjectID;
// i'd advice you rename context to avoid issues with flutter framework as its a reserved word
  List<String>? context;

  TaskItems(
      {this.taskName,
      this.status,
      this.projectName,
      this.parentProjectID,
      this.context});

  TaskItems.fromJson(Map<String, dynamic> json) {
    taskName = json['taskName'];
    status = json['status'];
    projectName = json['projectName'];
    parentProjectID = json['parentProjectID'];
    context = json['context'].cast<String>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['taskName'] = this.taskName;
    data['status'] = this.status;
    data['projectName'] = this.projectName;
    data['parentProjectID'] = this.parentProjectID;
    data['context'] = this.context;
    return data;
  }
}

in your list view

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(...
builder: ( context,
               snapshot) {
            if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
ListView.builder(
                
                  itemCount: snapshot.data!.length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    DocumentSnapshot? ds = snapshot.data![index];
                    final myTask=TaskItems.fromJson(
                            jsonDecode(jsonEncode(ds.data())));

// now you can access your tasks eg myTask.status


  • Related