Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

dart - Flutter SQFLITE turn Future List itterable

I'm struggling with a return of data from my DatabaseHelper and Sqflite.

This is how I'm querying my data:

Future<List<Message>> getMessages() async {
    final Database db = await database;
    final List<Map<String, dynamic>> maps = await db.query(table);
    
    return List.generate(maps.length, (i)  {
      return Message(
          sender: maps[i]['sender'],
          message: maps[i]['message'],
          date: maps[i]['date']
          );
    });
  }

In my main body of code i want to be able to iterate over it because I'm calling another class to format my messages.

class MessageBubble extends StatelessWidget {
  MessageBubble({this.sender, this.message, this.date, this.picture, this.isMe});

  final String message;
  final String sender;
  final String date;
  final Uint8List picture;
  final bool isMe;
....
}

So I need to be able to build my ListView Builder, but I can't figure out what to do here. How to I iterate over the Future<List> while calling MessageBubble for each returned row?

class ChatScreen extends StatefulWidget {
  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {

  var dbhelper = DatabaseHelper.instance;

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        appBar: AppBar(
        title: Text('Chat Screen'),
      ),
      body: new Container(
        padding: new EdgeInsets.all(16.0),
        child: new FutureBuilder<List<Message>>(
          future: dbhelper.getMessages(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return new ListView.builder(
                itemCount: snapshot.data.length,
                  itemBuilder: (context, index) {
                  //this is the section I dont' know how to handle
                  for (var m in snapshot.data){
                    MessageBubble(sender: m['sender']),
                  }
                    
                  );
                  });
            }
            return new Container(alignment: AlignmentDirectional.center
              ,);
          },
        ),
      )
    )
    );
  }
}
question from:https://stackoverflow.com/questions/65859094/flutter-sqflite-turn-future-list-itterable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I got the answer by doing this:

 if (snapshot.hasData) {
                    new ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        bubs.add(MessageBubble(
                               sender: snapshot.data[index].sender,
                                 message: snapshot.data[index].message,
                                 date: snapshot.data[index].date));
                        return ListView(
                          children:
                            bubs,
                        );
                      },
                    );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...