1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] type null is not a subtype of String error while fetching data from firebase...

Discussão em 'Mobile' iniciado por Stack, Outubro 25, 2024 às 11:12.

  1. Stack

    Stack Membro Participativo

    In my modelclass i have created named contructor OrderModel.fromSnapshot for converting firebase collection data to modalclass,

    So many times i am facing data convert errror,

    in my terminal i can see that fetchAllOrder is having error due to fromSnapshot constructor, but how to know which line is exactly the cause of error.?

    is there anytool or method to check where i am doing mistake


    Future<List<OrderModel>> fetchAllOrders() async{
    try{
    final snapshot=await FirebaseFirestore.instance.collection('Users').doc(FirebaseAuth.instance.currentUser!.uid).collection('Orders').orderBy('OrderDate',descending: true).get();
    final list=snapshot.docs.map((e) => OrderModel.fromSnapshot(e)).toList();
    return list;
    }
    catch(e){
    MyHelperFunction.showSnackbar(e.toString());
    throw e;
    }

    }


    here is my fromsnapshot() constructor, its difficult me to understand which property is not set properly....

    is there any way so that it specify which line is main cause of error?

    factory OrderModel.fromSnapshot(DocumentSnapshot<Map<String,dynamic>> snapshot){
    final data=snapshot.data() as Map<String,dynamic>;
    return OrderModel(
    orderId: snapshot.id,
    orderStatus: data['OrderStatus'],
    cart: (data['Cart'] as List<dynamic>).map((e) => CartItemModel.fromMap(e as Map<String,dynamic>)).toList(),
    orderAmount: data['OrderAmount'] as double,
    orderDate: (data['OrderDate'] as Timestamp).toDate(),
    paymentMethod: PaymentMethodModel.fromMap(data['PaymentMethod'] as Map<String,dynamic>),
    address: AddressModel.fromMap(data['Address'] as Map<String,dynamic>),
    delivaryDate: (data['DeliveryDate'])==null?null:(data['DeliveryDate'] as Timestamp).toDate(),
    );

    }

    Continue reading...

Compartilhe esta Página