Flutter Bloc : Fetch Firestore Data Into Model

496 views Asked by At

I'm trying to fetch data from Firestore into my model, but failed.

the error message says :

The method '[]' was called on null, tried calling : []("alamat")

Here is my codes :

Repositories :

Future<FaskesModel> getFaskesFirestore(String id) async {
    var result = _firestore
        .collection("fakses")
        .doc(id)
        .get()
        .then((snapshot) => FaskesModel.fromSnapshot(snapshot));
    return result;
  }

Class FaskesModel :

import 'package:cloud_firestore/cloud_firestore.dart';

class FaskesModel {
  String alamat;
  String id;
  String nama;
  GeoPoint alamatGeo;
  String deskripsi;
  String noTelepon;
  String noTeleponDarurat;
  String urlGambar;
  String website;

  FaskesModel(
      {this.alamat,
      this.alamatGeo,
      this.deskripsi,
      this.id,
      this.nama,
      this.noTelepon,
      this.noTeleponDarurat,
      this.urlGambar,
      this.website});

  factory FaskesModel.fromJson(Map<String, dynamic> json) => FaskesModel(
      alamat: json['alamat'] as String,
      alamatGeo: json['alamatGeo'] as GeoPoint,
      deskripsi: json['deskripsi'] as String,
      id: json['id'] as String,
      nama: json['nama'] as String,
      noTelepon: json['noTelepon'] as String,
      noTeleponDarurat: json['noTeleponDarurat'] as String,
      urlGambar: json['urlGambar'] as String,
      website: json['website'] as String);

  Map<String, dynamic> toJson() => {
        "alamat": alamat,
        "alamatGeo": alamatGeo,
        "deskripsi": deskripsi,
        "id": id,
        "nama": nama,
        "noTelepon": noTelepon,
        "noTeleponDarurat": noTeleponDarurat,
        "urlGambar": urlGambar,
        "website": website
      };

  FaskesModel.fromSnapshot(DocumentSnapshot documentSnapshot)
  
      : alamat = documentSnapshot.data()['alamat'],
        alamatGeo = documentSnapshot.data()['alamatGeo'],
        deskripsi = documentSnapshot.data()['deskripsi'],
        id = documentSnapshot.data()['id'],
        nama = documentSnapshot.data()['nama'],
        noTelepon = documentSnapshot.data()['noTelepon'],
        noTeleponDarurat = documentSnapshot.data()['noTeleponDarurat'],
        urlGambar = documentSnapshot.data()['urlGambar'],
        website = documentSnapshot.data()['urlGambar'];
}

and in the class Bloc, i just call like this :

 FaskesModel faskesModel =
           await  _firebaseRepository.getFaskesFirestore(event.id);

I have searched at another questions, i found to get the DataSnapshot field, just call snapShot.data.data but in my case i just found snapShot.data().

Once again, the point of my question is How to fetch data from Firestore into FaskesModel

I thank you.

1

There are 1 answers

0
gso_gabriel On

Posting this as Community Wiki, based in the comments.

It seems that the issue was related to a mistyping in the collection's name, when returning the data using a snapshot, to the class. Once the type was correct, the return of data worked correctly and there was no more null being printed.