Home > Enterprise >  After I have refreshed the page, the text inside the futurebuilder is not updating
After I have refreshed the page, the text inside the futurebuilder is not updating

Time:10-02

I am working with refreshing the data inside my page but after refreshing it is not updating unless I hot reload the application or restarting my app or log outing to the current user.

I can see that the data has been successfully updated in cloud firestore and the updated address is already printed in my console.

      class _TabPage3State extends State<TabPage3> {
        String myEmail = '';
        String fname = '';
        String lname = '';
        String? myAddress = '';
        int Bdate = 0;

        final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
            new GlobalKey<RefreshIndicatorState>();

        Future _fetch() async {
          final firebaseUser = await FirebaseAuth.instance.currentUser;

          if (firebaseUser != null) {
            await FirebaseFirestore.instance
                .collection('users')
                .doc(firebaseUser.email)
                .get()
                .then((ds) {
              myEmail = ds.data()!['email'] ?? '';
              fname = ds.data()!['first name'] ?? '';
              lname = ds.data()!['last name'] ?? '';
              myAddress = ds.data()!['address'] ?? '';
              Bdate = ds.data()!['age'] ?? '';
              print(myAddress);
            }).catchError((e) {
              print(e);
            });
          }
        }

        @override
        Widget build(BuildContext context) {
          print('TabPage3 build');
          return Scaffold(
            body: RefreshIndicator(
              key: _refreshIndicatorKey,
              onRefresh: _fetch,
              child: SafeArea(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      //address
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 25.0),
                        child: Container(
                          padding: EdgeInsets.all(15),
                          decoration: BoxDecoration(
                            color: bgColor,
                            borderRadius: BorderRadius.circular(12),
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            'address'.toUpperCase(),
                                            style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 16,
                                            ),
                                          ),
                                          SizedBox(
                                            height: 5,
                                          ),
                                          //subtitle
                                          FutureBuilder(
                                            future: _fetch(),
                                            builder: (context, snapshot) {
                                              if (snapshot.connectionState !=
                                                  ConnectionState.done) {
                                                return Text('loading...');
                                              }

                                              return Text(
                                                '$myAddress',
                                                softWrap: false,
                                                overflow: TextOverflow.ellipsis,
                                                style: TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                  color: grey,
                                                  fontSize: 14,
                                                ),
                                              );
                                            },
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              ),
                              IconButton(
                                onPressed: () async {
                                  Navigator.push(context,
                                      MaterialPageRoute(builder: (context) {
                                    return updateAddress();
                                  }));
                                },
                                icon: Icon(Icons.edit),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          );
        }
      }

CodePudding user response:

onRefresh: () { return Future(() { setState(() { _fetch }); });},

CodePudding user response:

Whenever using Future.builder try to use the snapshot data that you get from the future call. Also, try to use type annotations with Future.builder. Otherwise, you might end up with lots and lots of memory leaks.

CodePudding user response:

you should use

setState(() { _myState = newValue; });

method to rebuild widget with new Values read more about state in flutter dev setState-method

  • Related