简体   繁体   中英

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String?>'

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

You need to change this line

blockUid = doc['blockUid'];

to

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

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

please replace

blockUid = doc['blockUid'];

with this

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

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

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()
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM