List.add 无法正常工作

Posted

技术标签:

【中文标题】List.add 无法正常工作【英文标题】:List.add wont works flutter 【发布时间】:2021-11-06 21:35:43 【问题描述】:

EventDetails 类的代码

class EventDetails 
EventDetails(
      
      required this.description,
      required this.date,
      required this.startTime,
      required this.endTime,
      required this.speaker,
      required this.isFavorite);

  String description;
  String date;
  String startTime;
  String endTime;
  String speaker;
  String isFavorite;

网络类代码

class Network 
  static Future<List<EventDetails>> getEvents() async 
    Firebase.initializeApp();
    FirebaseFirestore instance = FirebaseFirestore.instance;
    var data = await instance.collection('event_details').get();
    return makeEvents(data.docs);
  

  static List<EventDetails> makeEvents(List<dynamic> collection) 
    List<EventDetails> events = [];
    collection.forEach((event) 
      print('++++++++++++++==========');
      print(event['description']);
      print(event['date']);
      print(event['start_time']);
      print(event['end_time']);
      print(event['speaker']);
      print(event['is_favorite']);

  events.add(
    EventDetails(
      description: event['description'],
      date: event['date'],
      startTime: event['start_time'],
      endTime: event['end_time'],
      speaker: event['speaker'],
      isFavorite: event['is_favorite'],
    ),
  );
);
print('--------');
print(events.length);
return events;
  

在上面的代码中,我试图在 getEvents 函数中从 cloud firestore 获取数据,并通过 makeEvents 函数转换为 EventDetails 来返回数据。问题出在我的 makeEvents 函数中,我在整个数据上设置了 forEach,首先我打印整个数据,然后添加到事件详细信息列表中,但在第一次迭代中打印了数据,但之后没有任何反应,并且列表或列表长度没有更新

我的构建方法

@override
  Widget build(BuildContext context) 
    return FutureBuilder<List<EventDetails>>(
      future: Network.getEvents(),
      builder: (context, snapshot) 
        if (snapshot.hasData) 
          return ListView.builder(
              itemCount: snapshot.data!.length,
              itemBuilder: (context, index) 
                return ListTile(
                  title: Text(snapshot.data![index].description),
                );
              );
        
        return const Center(
          child: CircularProgressIndicator(),
        );
      ,
    );
  

我的收藏结构

【问题讨论】:

分享你的收藏结构 问题已更新 分享代码,而不是图片。 我只是从 Gui 手动添加数据 在makeEvents中打印集合并查看结果 【参考方案1】:

首先,Firebase.initializeApp(); 是一个异步函数。你应该等待它。所以改成await Firebase.initializeApp();

其次,您应该将await Firebase.initializeApp(); 放在您的main() 中,而不是放在Network 类中。

试试这个。

class Network 
  static Future<List<EventDetails>> getEvents() async 
    // await Firebase.initializeApp();
    // move above to main() in main.dart
    FirebaseFirestore instance = FirebaseFirestore.instance;
    QuerySnapshot<EventDetails> query = await instance
        .collection('event_details')
        .withConverter<EventDetails>(
          fromFirestore: (snapshot, _) => EventDetails.fromFirestore(snapshot),
          toFirestore: (data, _) => data.toJson(),
        )
        .get();
    List<EventDetails> events = query.docs.map((e) => e.data()).toList();
    print(events);
    return events;
  

将此添加到您的事件模型中

factory EventDetails.fromFirestore(DocumentSnapshot<Map<String, dynamic>> snapshot) 
  Map<String, dynamic> json = snapshot.data();
  return EventDetails(
    id: snapshot.id,
    // The above gives you the document id in the model
    // you should add id to your model
    description: json['description'] as String,
    date: json['date'] as String,
    // startTime and endTime should be dateTime;
    startTime: json['start_time'],
    endTime: json['end_time'],
    // speaker should be map
    speaker: json['speaker'],
    isFavorite: json['is_favorite'] as bool,
  );

Map<String, dynamic> toJson() => <String, dynamic>
  'description': this.description,
  'date': this.date,
  'start_time': this.startTime,
  'end_time': this.endTime,
  'speaker': this.speaker,
  'is_favorite': this.isFavorite,
;

【讨论】:

以上是关于List.add 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

Java:将矩阵转换为列表:我的add方法不起作用,我不知道为什么

AVPlayer 无法正常工作 - Swift

为啥隐藏和显示密码功能无法正常工作?

xmppframework 的 didReceive 存在无法正常工作

Qt QToolBar 无法正常工作

剑道 DatePicker 文化无法正常工作