skip to Main Content

Below when I am printing print(snap[‘x’]); this works fine and gives me the data from firebase but when I am using the same data bellow title: Text(snap[‘x’]!), it a circular progress is shows.
please tell me some way to take the value of snap[‘x’], to title: Text(snap[‘x’]!), It will be very helpful.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

class ListPage extends StatefulWidget {
  const ListPage({super.key});

  @override
  State<ListPage> createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {
  @override
  Widget build(BuildContext context) {
    var snap;
    DocumentReference docRef = FirebaseFirestore.instance
        .collection('fruitsAndVegetable')
        .doc('0 Apple');
    docRef.get().then((DocumentSnapshot snapshot) async {
      print('================================================================');
      print(snapshot.data());
      if (snapshot.exists) {
        snap = snapshot.data();
        print(snap['x']);
      }
    });

    return Scaffold(
        appBar: AppBar(
          title: Text("some data"),
        ),
        body: snap != null
            ? Column(
                children: [
                  ListTile(
                    title: Text(snap['x']!),
                  ),
                  ListTile(
                    title: Text(snap['id']!),
                  ),
                  ListTile(
                    title: Text(snap['name']!),
                  ),
                ],
              )
            : Center(
                child: Center(
                  child: CircularProgressIndicator(),
                ),
              ));
  
  }
}

3

Answers


  1. the snap widget you specified will be on the build and its value is setState(() { snap = snapshot.data(); }); I hope it helped you use it as

    Setstate is work only inside statefulwidget

    Login or Signup to reply.
  2. Try Text(snap[‘x’].toString())

    Login or Signup to reply.
  3. import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter/material.dart';
    
    class ListPage extends StatefulWidget {
      const ListPage({super.key});
    
      @override
      State<ListPage> createState() => _ListPageState();
    }
    
    class _ListPageState extends State<ListPage> {
      late var snap;
      @override
      Widget build(BuildContext context) {
        DocumentReference docRef = FirebaseFirestore.instance
            .collection('fruitsAndVegetable')
            .doc('0 Apple');
        docRef.get().then((DocumentSnapshot snapshot) async {
          print('================================================================');
          print(snapshot.data());
          if (snapshot.exists) {
    setState((){
            snap = snapshot.data();
    });
            print(snap['x']);
          }
        });
    
        return Scaffold(
            appBar: AppBar(
              title: Text("some data"),
            ),
            body: snap != null
                ? Column(
                    children: [
                      ListTile(
                        title: Text(snap['x']!),
                      ),
                      ListTile(
                        title: Text(snap['id']!),
                      ),
                      ListTile(
                        title: Text(snap['name']!),
                      ),
                    ],
                  )
                : Center(
                    child: Center(
                      child: CircularProgressIndicator(),
                    ),
                  )
    );
    }
    }
    

    if you copy this code it will be work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search