Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm getting null when I fetch an array from cloud firestore

I am trying to fetch an array from the firestore and want to store it in a list but I keep getting an error " type ‘Null’ is not a subtype of type ‘List’ ". I have made sure the data inside the array is of String type. I have also made sure that the array is not null/empty.

this is the code implementation.

child: StreamBuilder<List<TableModel>>(
      stream: readTable(),
      builder: (context, snapshot) {
        if (snapshot.hasData){
          final table = snapshot.data;
          return ListView.builder(
            itemBuilder: (context, index) {
              String name = table![index].name;
              // print(name);
              bool serving = table[index].serving;
              // print(serving);
              if (serving){
                List<String> list = table[index].tableBill;
                print(list);

this is the error that I keep on receiving.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

type 'Null' is not a subtype of type 'List<String>'

TableModel Class

    class TableModel {
  var tableName;
  var tableBill;
  bool tableServing;

  TableModel({this.tableName, this.tableServing = false, this.tableBill});

  Future<bool> createNewTable()async {
    try {
      final newTable = FirebaseFirestore.instance.collection('Tables').doc('$tableName');
      final table = TableModel(tableName: tableName, tableServing: false, tableBill: []);
      final json = table.toJson();

      await newTable.set(json);

      return true;
    } 
    
    catch (e) {
      print(e);
      return false;
    }
  }


  // create entry in the database
  Future<bool> createTable() async{
    try{
      final docTable = FirebaseFirestore.instance.collection('Tables').doc('$tableName');

      final table = TableModel(tableName: tableName, tableServing: tableServing, tableBill: tableBill);

      final json = table.toJson();
      
      await docTable.set(json);
      return true;

    }
    catch(e){
      print(e);
      return false;
    }
  }

  

  Map<String,dynamic> toJson() => {
    'Table' : tableName,
    'Bill' : tableBill,
    'Serving' : tableServing
  };


  static TableModel fromJson(Map<String,dynamic> json){
    return TableModel(tableName: json['Table'], tableServing: json['Serving']);
  }

>Solution :

static TableModel fromJson(Map<String,dynamic> json){
    return TableModel(tableName: json['Table'], tableServing: json['Serving']);
  }

Fromjson method is missing tableBilling

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading