我试图显示我在名为“练习”的 Cloud Firestore 数据库中获取的资料的 ListView:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final db = FirebaseFirestore.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: db.collection('Exercises').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else
return ListView(
children: (snapshot.data! as QuerySnapshot).docs.map((doc) {
return Card(
child: ListTile(
title: Text(doc.data()['name']),
),
);
}).toList(),
);
},
),
);
}
}
但是当我这样做时,我的应用程序 中出现错误:错误影像
谢谢您的帮助
uj5u.com热心网友回复:
不能直接使用 StreamBuilder 到脚手架,添加 Column 或者 SinglechildScrollview 或者 Container。
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
final db = FirebaseFirestore.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column( // Column, Singlechild Scroll view Here
children: [
StreamBuilder<QuerySnapshot>(
stream: db.collection('Exercises').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
} else
return ListView(
children: (snapshot.data! as QuerySnapshot).docs.map((doc) {
return Card(
child: ListTile(
title: Text(doc.data()['name']),
),
);
}).toList(),
);
},
),
],
),
);
}
}
uj5u.com热心网友回复:
首先感谢您的回答。我用 Column 和 SinglechildScrollview 尝试过,但后来我得到另一个错误,即缺少 MediaQuery 小部件
媒体查询错误
0 评论