firebase - 如何在 Scoped Model 中从 Firestore 获取数据 - Flutter

标签 firebase dart flutter

我正在尝试从 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/

相关文章:

android - 无法解析符号 'FirebaseListAdapter'

node.js - Firebase存储使用云功能仅svg上传不起作用其他格式可以使用base64

flutter - 如何更新 firestore 文档中的字段?

Flutter 更改主题中所有凸起按钮的文本颜色

firebase - Firestore 是否对每个检索到的文档收取 Cloud Function Get 读取费用?

dart - 匿名函数的返回类型

asynchronous - Stream.last 回调从不触发

flutter - 从命名路由重定向用户

Flutter:ListView禁用触摸屏滚动

ios - 当我将它设置为每封电子邮件一个时,Firebase 创建多个帐户