0

Hi I was trying to load a future data from firebase in init state using my model UserModel.fromJson(Map<String,dynamic> user) but it keep throwing an error:

Null is not a sub type of Map<String, dynamic> 

I know the future is not loaded right away but that case in future.builder can be handled but how can I do that for values this is my code:

  @override
  void initState() {
    super.initState();
    checkForAdmin();
    print('settedBool:- $isAdmin');
  }

  Future<bool?> checkForAdmin() async {
    var crud = Provider.of<CRUDModel>(context, listen: false);
    var data = await crud.getUserById(id: widget.user!.uid);
    setState(() {
      isAdmin = data.isAdmin;
    });
    return data.isAdmin;
  }

If anyone have any idea or suggestions please share. Thank you.

import 'package:cloud_firestore/cloud_firestore.dart';

class UserModel {
  String? id;
  String? displayName;
  String? email;
  String? phoneNumber;
  String? photoUrl;
  bool? isAdmin = false;
  String? deviceToken;

  UserModel({
    this.id,
    this.displayName,
    this.email,
    this.phoneNumber,
    this.photoUrl,
    this.isAdmin,
    this.deviceToken,
  });

  UserModel.fromJson(Map<String?, dynamic>? data) {
    id = data!['id'] ?? '';
    displayName = data['displayName'] ?? '';
    email = data['email'] ?? '';
    phoneNumber = data['phoneNumber'] ?? '';
    photoUrl = data['photoUrl'] ?? '';
    isAdmin = data['isAdmin'] ?? false;
    deviceToken = data['deviceToken'] ?? "";
  }
  UserModel.fromFirestore(DocumentSnapshot snapshot) {
    Map doc = snapshot.data() as Map<dynamic, dynamic>;
    id = doc['id'] ?? '';
    displayName = doc['displayName'] ?? '';
    email = doc['email'] ?? '';
    phoneNumber = doc['phoneNumber'] ?? '';
    photoUrl = doc['photoUrl'] ?? '';
    isAdmin = doc['isAdmin'] ?? false;
    deviceToken = doc['deviceToken'] ?? '';
  }
  UserModel.fromFirestoreQuerySnapshot(QuerySnapshot snapshot) {
    snapshot.docs.map((doc) {
      return {
        id = doc['id'] ?? '',
        displayName = doc['displayName'] ?? '',
        email = doc['email'] ?? '',
        phoneNumber = doc['phoneNumber'] ?? '',
        photoUrl = doc['photoUrl'] ?? '',
        isAdmin = doc['isAdmin'] ?? false,
        deviceToken = doc['deviceToken'] ?? '',
      };
    });
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'displayName': displayName,
      'email': email,
      'phoneNumber': phoneNumber,
      'photoUrl': photoUrl,
      'isAdmin': isAdmin,
      'deviceToken': deviceToken,
    };
  }
}

This is my model class and this is the crud call

  Future<UserModel> getUserById({String? id}) async {
    var doc = await _api!.getUserById(
      id: id,
    );
    return UserModel.fromJson(doc.data() as Map<String, dynamic>);
  }
  • You are getting this error becuase your model is expecting a map but it is getting null values. Please check if there is any null data comming. And if so put a null check in model. If there is null value and u dont know how to put null check then post your model and i will answer it. Let me know if it helped. @PrimeAlexander – Noman khanbhai Nov 05 '21 at 23:03
  • hi @Nomankhanbhai i have edited my post and included my model and crud method is there anything i'm missing – Prime Alexander Nov 06 '21 at 13:47
  • Can you add full model class @PrimeAlexander – Noman khanbhai Nov 06 '21 at 18:01
  • i've edited it with the whole model class but the error always points to the crud method call – Prime Alexander Nov 06 '21 at 19:56
  • Okay now one last thing can you post the line on which this error is comming. (see the error in terminal and ctr+left click) so it will take you to the line on whuch error is comming. Share that line please @PrimeAlexander – Noman khanbhai Nov 07 '21 at 05:33
  • it take me to the crud method call and the line is "return UserModel.fromJson(doc.data() as Map);" line @Nomankhanbhai – Prime Alexander Nov 07 '21 at 08:38
  • try removing ```.data() as Map``` return only ``doc`` like ```UserModel.from(doc)``` @PrimeAlexander – Noman khanbhai Nov 07 '21 at 09:36
  • thanks a lot @Nomankhanbhai but that too have an error so i dropped this method and wrapped the widget i want to render with a future.builder method its more efficient and gives me control over the state of the data being received either if its's null or have a a data thanks again for your time – Prime Alexander Nov 08 '21 at 13:08

0 Answers0