我正在尝试从 Firestore 获取数据,在调试打印中, future 会完成它的工作,列表获取数据,在 debugPrint 中,长度为 +,但是当我尝试在另一个 Widget 列表中获取数据时,返回 null,在 debugPrint 中,长度为0 .
模型. Dart
class BBModel extends Model {
int _counter = 10;
int get counter => _counter;
var db = dbBB;
List<BB> _bbs;
List<BB> get bbs => _bbs;
Future<List<BB>> getBBs() async {
var snapshot = await db.getDocuments();
for (int i = 0; i < snapshot.documents.length; i++) {
_bbs.add(BB.fromSnapshot(snapshot.documents[i]));
print(bbs.length.toString()); //recives 23
}
notifyListeners();
return _bbs;
}
}
主.dart
void main() {
var model = BBModel();
model.getBBs();
runApp(ScopedModel<BBModel>(model: BBModel(), child: MyApp()));
}
statefullpage.dart
Expanded(
flex: 1,
child: Container(
height: 400.0,
child: ScopedModelDescendant<BBModel>(
builder: (context, child, model) {
return ListView.builder(
itemCount: model.bbs.length,
itemBuilder: (context, index) {
return Text(model.bbs[index].bbID);
});
}))),
最佳答案
看起来您在 main.dart 中编写的代码是错误的。实例化模型与您在 ScopedModel 中发送的模型不同。
更正
在您的 main.dart 文件中将 model: model
更改为 model: BBModel()
。
void main() {
final model = BBModel();
model.getBBs();
runApp(ScopedModel<BBModel>(model: model, child: MyApp()));
}
关于firebase - 如何在 Scoped Model 中从 Firestore 获取数据 - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54991280/