我是Flutter的新手。我有一个小项目有问题。我从Firebase数据库收集数据,并将其添加到listTile中。我正在尝试添加2个静态ListTile,但每个循环都将它们添加到主屏幕。我无法摆脱困境-任何愿意提供帮助的人。
为每个循环添加“ Datablade”和“ Vejledning”列表块
'''
import 'package:flutter/material.dart';
import 'package:flutter_app/services/auth.dart';
import 'package:flutter_app/services/database.dart';
import 'package:provider/provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
//import 'package:flutter_app/screens/home/brew_list.dart';
import 'package:flutter_app/models/brew.dart';
class Home extends StatelessWidget {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return StreamProvider<List<Brew>>.value(
value: DatabaseService().filer,
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Arbejdsmiljø'),
backgroundColor: Colors.orange[400],
elevation: 0.0,
actions: <Widget>[
FlatButton.icon(
icon: Icon(Icons.person),
label: Text('Log ud'),
onPressed: () async {
await _auth.signOut();
},
),
],
),
body:
ListPage(),
),
);
//ListPage(),
}
}
class ListPage extends StatefulWidget {
@override
_ListPageState createState() => _ListPageState();
}
class _ListPageState extends State<ListPage> {
Future _data;
Future getPosts() async{
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection('filer').getDocuments();
return qn.documents;
}
navigateToDetail(DocumentSnapshot post) {
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailPage(post: post,)));
}
void initState() {
super.initState();
_data = getPosts();
}
@override
Widget build(BuildContext context) {
return Container(
child: FutureBuilder(
future: _data,
builder: (_, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: Text('Loading'),
);
} else {
return ListView.builder(
//return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (_, index){
return Card(
child: Column(
children: <Widget> [
ListTile(
leading: Icon(Icons.star, color: Colors.orange, size: 26.0),
onTap: () => navigateToDetail(snapshot.data[index]),
title: Text('Datablade'),
trailing: Icon(Icons.keyboard_arrow_right),
),
Divider(color: Colors.orange,indent: 16.0),
ListTile(
title: new Center(child: new Text("Vejledninger",
style: new TextStyle(
color: Colors.orange[400],
fontWeight: FontWeight.w500, fontSize: 25.0),)),
),
ListTile(
onTap: () => navigateToDetail(snapshot.data[index]),
title: Text(snapshot.data[index].data["name"]),
leading: CircleAvatar(
backgroundColor: Colors.orange[400],
foregroundColor: Colors.white,
child: Text(snapshot.data[index].data["name"][0])
)
)
],
),
);
//return ListTile(
//title: Text(snapshot.data[index].data["name"]),
//onTap: () => navigateToDetail(snapshot.data[index]),
//);
});
}
}),
);
}
}
class DetailPage extends StatefulWidget {
final DocumentSnapshot post;
DetailPage ({this.post});
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.post.data["name"]),
),
body: Container(
child: Card(
child: ListTile(
title: Text(widget.post.data["name"]),
subtitle: Text(widget.post.data['beskrivelse']),
),
),
),
);
}
}
'''