简体   繁体   English

FirebaseFirestore.get 返回 null - Flutter

[英]FirebaseFirestore.get returning null - Fluter

I'm trying to fetch data from Firestore.我正在尝试从 Firestore 获取数据。

class _TestState extends State<Test> {
bool abcd;
final adsRef = FirebaseFirestore.instance.collection('All ads');
getData(){
adsRef.get().then((snap) {
  snap.docs.forEach((doc) { 
    abcd=doc.data().isEmpty;
  });
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: new AppBar(
    title: Text("TestPage"),
  ),
  body: FlatButton(
    onPressed: () {
      print(abcd);
    },
    child: Text("data"),
  ),
  );
  }
  }

Here's the DB:这是数据库: D B

And I've tried getting doc.id , doc.exists , etc我试过获取doc.iddoc.exists
but It's returning 'null'.但它返回“空”。

PS: I'm a newbie to firebase. PS:我是firebase的新手。

Data is loaded from Firebase/Firestore (and most modern cloud APIs) asynchronously, and needs to be passed to the UI through a setState() call (or another state management primitive).数据从 Firebase/Firestore(和大多数现代云 API)异步加载,需要通过setState()调用(或其他状态管理原语)传递到 UI。

You should:你应该:

  1. start loading the data in the initState method of the class在类的initState方法中开始加载数据
  2. then call setState() when the data is available (or changes)然后在数据可用(或更改)时调用setState() )
  3. this will trigger the build() method, where you can then use the data from the state这将触发build()方法,然后您可以在其中使用状态中的数据

So something like:所以像:

class _TestState extends State<Test> {
  bool abcd;
  final adsRef = FirebaseFirestore.instance.collection('All ads');

  @override
  void initState() {
    super.initState();

    adsRef.get().then((snap) {
      snap.docs.forEach((doc) { 
        setState(() {
          abcd=doc.data().isEmpty;
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: Text("TestPage"),
      ),
      body: FlatButton(
        onPressed: () {
          print(abcd);
        },
        child: Text("data"),
      ),
    );
  }
}

You need to use "Async/Await" to get data from firebase您需要使用“Async/Await”从 firebase 获取数据

async getData()异步获取数据()

and call并打电话

data = await getData() like this. data = await getData() 像这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Null 检查运算符用于 null 值 FirebaseFirestore - Null check operator used on a null value FirebaseFirestore FirebaseFirestore错误<FirebaseOptions.getProjectId() cannot be null> - FirebaseFirestore error <FirebaseOptions.getProjectId() cannot be null> FirebaseFirestore.getInstance()给出Null指针异常 - FirebaseFirestore.getInstance() gives Null Pointer Exception 在返回 FirebaseFirestore QuerySnapshot 时使用 async* 和 yield* 有什么区别吗 - Is there any difference between using async* and yield* or not on returning FirebaseFirestore QuerySnapshot Flutter FirebaseFirestore where 条件返回相关和不相关的值 - Flutter FirebaseFirestore where condition returning related and unrelated values Firestore-如何从FirebaseFirestore.QuerySnapshot获取文档ID? - Firestore - How to get the doc id from FirebaseFirestore.QuerySnapshot? 如何从firebasefirestore获取Flutter中随机或未知的文档ID? - How to get a doc Id which is random or unknown in Flutter from firebasefirestore? 参数“firebaseFirestore”因其类型而不能具有“null”值,但 flutter 中的隐式默认值为“null” - The parameter 'firebaseFirestore' can't have a value of 'null' because of its type, but the implicit default value is 'null' in flutter Flutter Firestore 文档返回 null - Flutter Firestore doc get returning null 没有这样的模块&#39;FirebaseFirestore&#39; - No such module 'FirebaseFirestore'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM