Flutter Dart - Class 没有实例 getter。 Return 在 FutureBuilder 上

Flutter Dart - Class has no Instance getter. Return on FutureBuilder

你能帮我解决这个问题吗?我正在尝试从存储在 sqflite 中的数据 return 一个 FutureBuilder,return 它并用它构建一个小部件列表,但我有一个小问题,我不知道如何return 正确的列表:

我得到的错误是:

The following NoSuchMethodError was thrown building FutureBuilder<List>(dirty, state: _FutureBuilderState<List>#7e2ff): Class 'List' has no instance getter 'messageToJson'. Receiver: Instance(length:8) of '_GrowableList' Tried calling: messageToJson

这是我的留言 class:

import "dart:convert";

List<Message> messageFromJson(String str) =>
    List<Message>.from(json.decode(str).map((x) => Message.fromJson(x)));

String messageToJson(List<Message> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Message {
  String message;
  String sender;
  String date;
  String contactID;
  Message({this.sender, this.message, this.date, this.contactID});

  factory Message.fromJson(Map<String, dynamic> json) => Message(
      message: json['message'],
      sender: json['sender'],
      date: json['date'],
      contactID: json['contactID'],
  );

  Map<String, dynamic> toJson() => {
    "message": message,
    "sender" : sender,
    "date" : date,
    "contactID" : contactID
  }; 
}

我是这样称呼它的。我的问题在于我在哪里打电话并试图使用快照数据。然后我实例化 BeautifulBubble class,我认为这没问题。

body: new FutureBuilder(
    future: dbHelper.getMessages(0),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return Center(
          child: CircularProgressIndicator(
            backgroundColor: Colors.lightBlueAccent,
          ),
        );
      }
    //Todo: Figure out how to call this:
      final messages = snapshot.data.messageToJson;
      List<BeautifulChatBubble> messageBubs = [];
      for (var message in messages.toJson().values) {
        final senderText = message.data['sender'];
        final messageText = message.data['message'];
        final dateText = message.data['date'];
        final contactIDText = message.data['contactID'];

        final messagebubble = BeautifulChatBubble(
          sender: senderText,
          text: messageText,
          date: dateText,
          isMe: false,
        );
        messageBubs.add(messagebubble);
      }
      return Expanded(
        child: ListView(
          reverse: true,
          padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
          children: messageBubs,
        ),
      );
    }

  )

同时添加 dbhelper.getmessages

  Future<List<Message>> getMessages(int chatid) async {
    final db = await database;
    final res = await db.query(messagetable, where: '$columnChatID = ?', whereArgs: [chatid]);
    List<Message> messagelist =
        res.isNotEmpty? res.map((c) => Message.fromJson(c)).toList() : [] ;

    return messagelist;
  }

getMessages() 方法 return 实体列表,而不是单个实体。在您的案例中,数据库将数据解析为 Message class 并且您应该将来自 FutureBuilder 的数据作为 Message class 实例进行操作。

final messages = snapshot.data;
List<BeautifulChatBubble> messageBubs = [];
for (var message in messages) {
  final senderText = message.sender;
  final messageText = message.message;
  final dateText = message.date;
  final contactIDText = message.contactID;

  final messagebubble = BeautifulChatBubble(
    sender: senderText,
    text: messageText,
    date: dateText,
    isMe: false,
  );
  messageBubs.add(messagebubble);
}