Firebase 如何在范围模型中从Firestore获取数据-颤振

Firebase 如何在范围模型中从Firestore获取数据-颤振,firebase,dart,flutter,Firebase,Dart,Flutter,我正在尝试从Firestore获取数据,在调试打印future中执行任务,列表获取数据,在调试打印中长度为+,但当我尝试在另一个小部件列表中获取数据时,列表接收空值,在调试打印中长度为0 省道模型 class BBModel extends Model { int _counter = 10; int get counter => _counter; var db = dbBB; List<BB> _bbs; List<BB> get

我正在尝试从Firestore获取数据,在调试打印future中执行任务,列表获取数据,在调试打印中长度为+,但当我尝试在另一个小部件列表中获取数据时,列表接收空值,在调试打印中长度为0

省道模型

  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;
  }
}
BBModel类扩展了模型{
int_计数器=10;
int get counter=>\u计数器;
var-db=dbBB;
名单(bbs),;
列表获取bbs=>\u bbs;
Future getBBs()异步{
var snapshot=await db.getDocuments();
对于(int i=0;i
主飞镖

void main() {
  var model = BBModel();

  model.getBBs();
  runApp(ScopedModel<BBModel>(model: BBModel(), child: MyApp()));
}
void main(){
var模型=BBModel();
model.getBBs();
runApp(ScopedModel(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);
  });
 }))),
扩展(
弹性:1,
子:容器(
高度:400.0,
子项:ScopedModelSecondant(
生成器:(上下文、子对象、模型){
返回ListView.builder(
itemCount:model.bbs.length,
itemBuilder:(上下文,索引){
返回文本(model.bbs[index].bbID);
});
}))),

在main.dart中,我会尝试执行以下操作:

void main() {
  var model = BBModel();

  model.getBBs().then((someVariableName){
    runApp(ScopedModel<BBModel>(model: BBModel(), child: MyApp()));
  });
}
void main(){
var模型=BBModel();
model.getBBs().then((someVariableName){
runApp(ScopedModel(model:BBModel(),child:MyApp());
});
}

注意:“someVariableName”将包含一个列表

看起来您在main.dart中编写的代码是错误的。已安装的模型与您在ScopedModel中发送的模型不同

校正

将main.dart文件中的
model:model
更改为
model:BBModel()

void main() {
  final model = BBModel();    

  model.getBBs();
  runApp(ScopedModel<BBModel>(model: model, child: MyApp()));
}
void main(){
最终模型=BBModel();
model.getBBs();
runApp(ScopedModel(model:model,child:MyApp());
}

要等待,您可以使用

await model.getBBs();
但是,除此之外,我不建议将数据上传到主应用程序,因为数据越来越大,这会降低应用程序的使用速度。只将数据上传到您需要的页面,并找到一种方法