【问题标题】:My data from firestore not retrieve using dart flutter我从 Firestore 中的数据无法使用 dart Flutter 检索
【发布时间】:2020-05-16 10:38:05
【问题描述】:

我在颤振中使用云火存储:

  1. 已将应用与 Firebase 连接
  2. 将 google-services.json 放入 app buil.gradle 中
  3. 添加依赖和插件
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  MyApp();
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: 'کوردی پۆلی یەک',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      home: MainScreen(),
    );
  }
}
class MainScreen extends StatelessWidget{

  List<Widget> makeListWidget(AsyncSnapshot snapshot){
    return snapshot.data.documents.map<Widget>((document){
      return ListTile(
        title: Text(document["name"]),
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("کوردی پۆلی یەک",style: TextStyle(color: Colors.white),),
        backgroundColor: Colors.deepOrange,
      ),
      body: Container(
        child: StreamBuilder(
          stream: Firestore.instance.collection('lesson').snapshots(),
          builder: (context,snapshot){
            switch(snapshot.connectionState){
              case ConnectionState.none:
                return Center(child: Text('No data'));
              case ConnectionState.waiting:
                return Center(
                    child:CircularProgressIndicator());
              default:
                return ListView(
                  children:makeListWidget(snapshot),
                );
            }
          },
        ),
      ),
    );
  }
}

它只是在加载,当我删除 connectionState.waiting 时会出现错误:

NoSuchMethodError:在 null 上调用了 getter 'documents'。接收方:空 尝试调用

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore firebase-security


    【解决方案1】:

    将 Firestore 安全规则更改为以下内容:

    // Allow read/write access to all users under any conditions
    // Warning: **NEVER** use this rule set in production; it allows
    // anyone to overwrite your entire database.
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    【讨论】:

    • 谢谢@Peter Haddad 我已经这样做了,它不起作用
    • 你能打印快照吗? print(snapshot) 在方法makeListWidget(AsyncSnapshot
    • 我只是重新创建了项目,但这次我在测试模式下尝试了它,它现在正在使用相同的代码,但是当我想将其更改为实模式时,我将面临什么? @彼得哈达德
    • @Asia 我不确定,但你需要做的是调试这个问题,你可以添加断点或使用 print(snapshot) 看看你得到了什么
    • 非常感谢,我意识到另一个解决方案是更改权限允许读取,写入:如果为真;允许读、写;
    【解决方案2】:

    更改允许读、写:如果为真;允许读、写;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2020-10-30
      • 2021-04-17
      • 1970-01-01
      • 2019-09-24
      • 1970-01-01
      相关资源
      最近更新 更多