简体   繁体   中英

Flutter --> Listile and Streambuilder

class Messages extends StatelessWidget {
 const Messages ({Key key}) : super(key: key);

 @override
Widget build(BuildContext context) {
return StreamBuilder(
  stream: Firestore.instance.collection("messages").snapshots(),
  builder: (context, snapshot) {
    if (snapshot.hasError) {
      return Text("Errror ${snapshot.error}");
    }
    if (snapshot.connectionState == ConnectionState.waiting) {
      return CircularProgressIndicator();
    }
    return ListView(
      children: snapshot.data.documents
          .map<Widget>((doc) => ListTile(
                title: Text(doc['name']),
                subtitle: Text(doc['message']),
              ))
          .toList(),
    );
  },
);
}
}

Hello everyone, we can pull data from firebase. We can print to Debugprint. But when we try to print on the screen using the list, we get an error. Can you help me?

在此处输入图像描述

As the error says, you need to wrap the ListTile widget with a Material Widget:

children: snapshot.data.documents
          .map<Widget>((doc) => Card(child: ListTile(
                title: Text(doc['name']),
                subtitle: Text(doc['message']),
              )))
          .toList(),

if you read what the error is telling you you might find the answer to this. in flutter material, normally widgets must be placed on top of some other widgets. just like we put tiles after we concreted the floor. so the concrete provides a base for the tiles.

what you are missing is that the listview is not on top of a base material widget. to fix that simply wrap them in a Scaffold or any similar base widgets. if you are using scaffold use it as the root widget.

like this,

Widget build(BuildContext context) {
  return Scaffold(
      body : //Your widget
    )

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