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

Time:07-10

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.

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']);
  }

CodePudding user response:

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

Fromjson method is missing tableBilling

  • Related