【问题标题】:How to retrieve data from firebase straight into the app?如何从firebase直接检索数据到应用程序中?
【发布时间】:2022-01-18 00:57:42
【问题描述】:

我是新来的颤振。我想从 firebase firestore 数据库中检索数据并将其显示在应用程序中。我的要求是在屏幕上以文本的形式显示数据。为了更好地理解,我附上了下面的代码。

注意:我已经在我的项目中配置了 firebase。

代码:

  class dashboard extends StatefulWidget {
  @override
  _dashboardState createState() => _dashboardState();
}

// ignore: camel_case_types
class _dashboardState extends State<dashboard> {
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    MediaQueryData screen = MediaQuery.of(context);
    final authService = Provider.of<AuthService>(context);
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 80.0, right: 250),
              child: Center(
                child: Container(
                  width: screen.size.width,
                  // height: 20.0,
                  decoration:
                      BoxDecoration(borderRadius: BorderRadius.circular(15.0)),
                  child: (const Text(
                    'I need the data retreievd from firebase firestore to be displayed here.',
                    textAlign: TextAlign.justify,
                    style: TextStyle(
                        fontWeight: FontWeight.bold, color: Colors.black),
                  )),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 300.0, top: 1.0),
              child: IconButton(
                icon: new Icon(Icons.account_circle, size: 30.0),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => ProfilePage(),
                    ),
                  );
                },
              ),
            ),
            Padding(
              padding: EdgeInsets.only(left: 300.0, top: 5.0),
              child: IconButton(
                icon: const Icon(
                  Icons.notifications,
                  size: 25.0,
                ),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => Notifications(),
                    ),
                  );
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 0.0),
              child: Center(
                child: Container(
                  width: 390,
                  height: 450,
                  decoration: BoxDecoration(
                    color: Colors.green.shade100,
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(onPressed: () async {
        await authService.signOut();
      }),
      //  : _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.green[100],
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.book_online),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.read_more),
            label: 'Settings',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.account_circle),
            label: 'Profile',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.green[800],
        onTap: _onItemTapped,
      ),
    );
  }
}

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore mobile-application


    【解决方案1】:

    您可以从 firebase firestore 数据库中检索数据并将其直接显示在应用程序中。

    SteamBuilder(
    steam:query(FirebaseFirestore.instance.collection('collection_name').
    doc('doc_id').snapshot());
    builder(context,snapshot){
    return Text(snapshot.data.docs[0].get('field');
    }
    )
    

    【讨论】:

    • 嗨,我尝试将此添加到我的代码中。但是我在尝试将其添加到我的代码中时遇到了一些错误。
    • 请描述什么样的错误,以便我可以帮助你。
    • 当然!错误 1:命名参数 'builder' 是必需的,但没有对应的参数。尝试添加所需的参数。dartmissing_required_argument 错误 2:没有为类型“_dashboardState”定义方法“查询”。尝试将名称更正为现有方法的名称,或定义名为“查询”的方法。dartundefined_method
    • 我已经写了查询词,所以你可以在它下面。换句话说,它会。 SteamBuilder(steam:FirebaseFirestore.instance.collection('collection_name').doc('doc_id').snapshot(); builder(context,snapshot){ return Text(snapshot.data.docs[0].get('field' ); } )
    • 是 StreamBuilder 还是 SteamBuilder?
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2020-04-16
    相关资源
    最近更新 更多