Firebase Documentsnapshot map to Json object in Flutter, help needed

4,483 views
Skip to first unread message

David Escobar

unread,
Dec 11, 2019, 9:18:35 PM12/11/19
to Flutter Development (flutter-dev)

I am trying to map a DocumentSnap from Firebase to a Json Class in Flutter. I can confirm that my is getting the data from the document, but i cannot pass the gathered data to my Json object. I have attached code and error message.

Class for getting the document from Firebase DatabaseService.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

import 'package:oast_app/widgets/auth_class.dart';



class FireTest {
  Future<List<UserCheck>> streamUser(FirebaseUser user) async {
    DocumentSnapshot querySnapshot =
        await Firestore.instance.collection('users').document(user.uid).get();
    if (querySnapshot.exists) {
      print('success');
      print(querySnapshot.data['fname']);
      print(querySnapshot.data['lname']);

      querySnapshot.data.map<String, dynamic>((json) => UserCheck.fromJson(json)); **this is whats erroring**
    }
    return [];
  }
}

Json Object class

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';

part 'auth_class.g.dart';

@JsonSerializable(nullable: false)
class UserCheck extends ChangeNotifier {
  final String uid;
  final String fName;
  final String lName;
  final String regEmail;
  final String org;

  UserCheck({
    this.uid,
    this.fName,
    this.lName,
    this.regEmail,
    this.org,
  });

  factory UserCheck.fromJson(Map<String, dynamic> json) =>
      _$UserCheckFromJson(json);

  Map<String, dynamic> toJson() => _$UserCheckToJson(this);
}

Error Message

Compiler message: lib/widgets/database_service.dart:17:76: Error: The argument type 'String' can't be assigned to the parameter type 'Map'. - 'Map' is from 'dart:core'. querySnapshot.data.map((json) => UserCheck.fromJson(json)); ^ lib/widgets/database_service.dart:17:67: Error: A value of type 'UserCheck' can't be assigned to a variable of type 'MapEntry'. - 'UserCheck' is from 'package:oast_app/widgets/auth_class.dart' ('lib/widgets/auth_class.dart'). - 'MapEntry' is from 'dart:core'. querySnapshot.data.map((json) => UserCheck.fromJson(json)); ^ lib/widgets/database_service.dart:17:47: Error: The argument type 'MapEntry Function(String)' can't be assigned to the parameter type 'MapEntry Function(String, dynamic)'. - 'MapEntry' is from 'dart:core'. querySnapshot.data.map((json) => UserCheck.fromJson(json)); ^

Reply all
Reply to author
Forward
0 new messages