Home > database >  Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Str
Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Str

Time:07-01

I got an error when I fetch the array data from FireStore. I added "blockUid" field on FireStore. but It's not working, despite the others are all working.

github:https://github.com/ATUSHIKADOWAKI/dance_4_life/tree/main/lib

(main_model.dart)

Future<void> fetchEvents() async {
    final docs = await FirebaseFirestore.instance
        .collection('event')
        .orderBy('timestamp', descending: true)
        .get();

    final events = docs.docs.map((doc) => Events(doc)).toList();
    this.events = events;

    notifyListeners();
  }

(events.dart)

class Events {
  String? eventId;
  String? title;
  String? date;
  String? imgURL;
  String? detail;
  //array is here.
  List<String?> blockUid = [];
  String? eventPlace;
  String? eventAddress;
  String? eventCategory;
  String? eventPrice;
  String? eventGenre;

  Events(DocumentSnapshot doc) {
    eventId = doc.id;
    title = doc['title'];
    eventPlace = doc['eventPlace'];
    eventAddress = doc['eventAddress'];
    eventCategory = doc['eventCategory'];
    eventPrice = doc['eventPrice'];
    eventGenre = doc['eventGenre'];
    date = doc['date'];
    imgURL = doc['imgURL'];
    detail = doc['detail'];
    blockUid = doc['blockUid'];
  }
}

CodePudding user response:

You need to change this line

blockUid = doc['blockUid'];

to

 blockUid = doc['blockUid'] as List<String>;

CodePudding user response:

please replace

blockUid = doc['blockUid'];

with this

blockUid = List<String>.from(doc["data"].map((x) => x) ?? [])

CodePudding user response:

Make sure all the data in the blockUid list in Firestore is String and try this:

Future<void> fetchEvents() async {
    final docs = await FirebaseFirestore.instance
        .collection('event')
        .orderBy('timestamp', descending: true)
        .get();

    final events = docs.docs.map((doc) => Events.fromDoc(doc)).toList();
    this.events = events;

    notifyListeners();
  }
class Events {

  Events({
    this.eventId,
    this.title,
    this.date,
    this.imgURL,
    this.detail,
    this.blockUid,
    this.eventPlace,
    this.eventAddress,
    this.eventCategory,
    this.eventPrice,
    this.eventGenre,
});
  String? eventId;
  String? title;
  String? date;
  String? imgURL;
  String? detail;
  //array is here.
  List<String>? blockUid;
  String? eventPlace;
  String? eventAddress;
  String? eventCategory;
  String? eventPrice;
  String? eventGenre;

  factory Events.fromDoc(DocumentSnapshot doc) => Events(
    eventId: doc.id,
    title: doc['title'],
    eventPlace: doc['eventPlace'],
    eventAddress: doc['eventAddress'],
    eventCategory: doc['eventCategory'],
    eventPrice: doc['eventPrice'],
    eventGenre: doc['eventGenre'],
    date: doc['date'],
    imgURL: doc['imgURL'],
    detail: doc['detail'],
    blockUid: doc['blockUid'],
  );
}

CodePudding user response:

You only need to do Replace this List<dynamic> blockUid = [];

CodePudding user response:

Cast individual item to String, something like this:

  Events(DocumentSnapshot doc) {
    eventId = doc.id;
    title = doc['title'];
...
    blockUid = doc['blockUid'].map((item) => item as String).toList()
  }
  • Related