title: Flutter : The earnings screen in my driver app in flutter does not change the state of it in realtime using firebase?? .. I want a solution please
body: <p>My app screen does not change the state of it in realtime while updated in firebase realtime database .. the earnings screen is updated after the driver end the trip and collect the fares of the trip then updated total earnings and number of total trips and the history of trips .. I want a solution</p>
  <p>Earnings class</p>
  <pre><code>import 'package:drivers_app/Data_Handler/app_Data.dart';
  import 'package:drivers_app/modules/history_screen.dart';
  import 'package:drivers_app/shared/components/components.dart';
  import 'package:flutter/material.dart';
  import 'package:provider/provider.dart';
  
  class EarningsScreen extends StatelessWidget {
  
    @override
    Widget build(BuildContext context) {
      return Column
      (
        children:
        [
          Container(
            color: Colors.black87,
            width: double.infinity,
            child: Padding(
                padding: EdgeInsets.symmetric(vertical: 50.0),
                child: Column(
                  children:
                  [
                    Text(&quot;Total Earnings&quot;, style: TextStyle(fontSize: 18.0 ,color: Colors.white),),
                    SizedBox(height: 30.0,),
                    Text(&quot;${Provider.of&lt;AppData&gt;(context, listen: false).earnings} L.E&quot;, style: TextStyle(color: Colors.white, fontSize: 50),),
                  ],
                ),
            ),
          ),
  
          MaterialButton(
              padding: EdgeInsets.all(0),
              onPressed: ()
              {
                navigateTo(context, HistoryScreen());
              },
              child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: 30, vertical: 18),
                child: Row(
                  children:
                  [
                    Image.asset('assets/images/taxi.png', width: 70,),
                    SizedBox(width: 16.0,),
                    Text(&quot;Total Trips&quot;, style: TextStyle(fontSize: 16.0),),
                    Expanded(child: Container(child: Text(Provider.of&lt;AppData&gt;(context, listen: false).countTrips.toString(), textAlign: TextAlign.end, style: TextStyle(fontSize: 18.0),))),
                  ],
                ),
              ),
          ),
          Divider(height: 2.0, thickness: 2.0,),
        ],
      );
    }
  }
  </code></pre>
  <p>AppData class</p>
  <pre><code>// ignore_for_file: file_names
  import 'package:drivers_app/models/address.dart';
  import 'package:drivers_app/models/drivers.dart';
  import 'package:drivers_app/models/history.dart';
  import 'package:flutter/material.dart';
  
  class AppData extends ChangeNotifier
  {
    String earnings = &quot;0&quot;;
    int countTrips = 0;
    Address myAddress = Address();
    List&lt;String&gt; tripHistoryKeys = [];
    List&lt;History&gt; tripHistoryDataList = [];
  
    void updateEarnings(String updatedEarnings)
    {
      earnings = updatedEarnings;
      notifyListeners();
    }
  
    void updateTripsCounter(int tripCounter)
    {
      countTrips = tripCounter;
      notifyListeners();
    }
  
    void updateTripsKeys(List&lt;String&gt; newKeys)
    {
      tripHistoryKeys = newKeys;
      notifyListeners();
    }
  
    void updateTripHistoryData(History eachHistory)
    {
      tripHistoryDataList.add(eachHistory);
      notifyListeners();
    }
  
    void updateMyAddress(Address myAddress){
      this.myAddress = myAddress;
      notifyListeners();
    }
  }
  </code></pre>
  <p>MainScreen class</p>
  <pre><code>import 'package:drivers_app/tabsPages/earnings_tab_screen.dart';
  import 'package:drivers_app/tabsPages/home_tab_screen.dart';
  import 'package:drivers_app/tabsPages/profile_tab_screen.dart';
  import 'package:drivers_app/tabsPages/zones_tab_page.dart';
  import 'package:flutter/material.dart';
  
  class MainScreen extends StatefulWidget
  {
    static const String mainScreenId = &quot;mainScreenId&quot;;
  
    @override
    _MainScreenState createState() =&gt; _MainScreenState();
  }
  
  class _MainScreenState extends State&lt;MainScreen&gt; with SingleTickerProviderStateMixin
  {
    int selectedIndex = 0;
  
    void onItemClicked(int index)
    {
      setState(()
      {
        selectedIndex = index;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold
      (
        body: IndexedStack(
          children:
          [
            HomeScreen(),
            EarningsScreen(),
            ZonesScreen(),
            ProfileScreen(),
          ],
          index: selectedIndex,
        ),
        bottomNavigationBar: BottomNavigationBar(
          items:&lt;BottomNavigationBarItem&gt;
          [
            BottomNavigationBarItem(
              icon: Icon(
                  Icons.home,
                ),
              label: 'Home',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                  Icons.credit_card,
                ),
              label: 'Earnings',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                  Icons.settings_input_antenna_rounded,
                ),
              label: 'Zones',
            ),
            BottomNavigationBarItem(
              icon: Icon(
                  Icons.person,
                ),
              label: 'Account',
            ),
          ],
          unselectedItemColor: Colors.black54,
          selectedItemColor: Colors.blue,
          type: BottomNavigationBarType.fixed,
          selectedLabelStyle: TextStyle(
            fontSize: 12.0
          ),
          showUnselectedLabels: true,
          currentIndex: selectedIndex,
          onTap: onItemClicked,
  
        ),
      );
    }
  }
  </code></pre>
  <p>History Screen class</p>
  <pre><code>import 'package:drivers_app/Data_Handler/app_Data.dart';
  import 'package:drivers_app/shared/components/history_item.dart';
  import 'package:flutter/material.dart';
  import 'package:provider/provider.dart';
  
  class HistoryScreen extends StatefulWidget
  {
  
    @override
    _HistoryScreenState createState() =&gt; _HistoryScreenState();
  }
  
  class _HistoryScreenState extends State&lt;HistoryScreen&gt;
  {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Trip History'),
          backgroundColor: Colors.black87,
          leading: IconButton(
            onPressed: ()
            {
              Navigator.pop(context);
            },
            icon: Icon(Icons.keyboard_arrow_left),
          ),
        ),
        body: ListView.separated(
            itemBuilder: (context, index)
            {
              return HistoryItem(
                history: Provider.of&lt;AppData&gt;(context, listen: false).tripHistoryDataList[index],
              );
            },
            separatorBuilder: (BuildContext context, int index) =&gt; Divider(thickness: 2.0, height: 3.0,),
            itemCount: Provider.of&lt;AppData&gt;(context, listen: false).tripHistoryDataList.length,
            physics: ClampingScrollPhysics(),
            shrinkWrap: true,
        ),
      );
    }
  }
  </code></pre>
  <p>Functions class</p>
  <pre><code>static void retrieveHistoryInfo(context)
    {
      //retrieve and display Earnings
      driversRef.child(currentfirebaseUser!.uid).child(&quot;earnings&quot;).once().then((DataSnapshot dataSnapshot)
      {
        if(dataSnapshot.value != null)
        {
          String earnings = dataSnapshot.value.toString();
          Provider.of&lt;AppData&gt;(context, listen: false).updateEarnings(earnings);
        }
      });
  
      //retrieve and display Trip History
      driversRef.child(currentfirebaseUser!.uid).child(&quot;history&quot;).once().then((DataSnapshot dataSnapshot)
      {
        if(dataSnapshot.value != null)
        {
          //update total number of trip counts to provide
          Map&lt;dynamic, dynamic&gt; keys = dataSnapshot.value;
          int tripCounter = keys.length;
          Provider.of&lt;AppData&gt;(context, listen: false).updateTripsCounter(tripCounter);
  
          //update trip keys to provider
          List&lt;String&gt; tripHistoryKeys = [];
          keys.forEach((key, value) 
          {
            tripHistoryKeys.add(key);
          });
          Provider.of&lt;AppData&gt;(context, listen: false).updateTripsKeys(tripHistoryKeys);
          obtainTripRequestHistoryData(context);
        }
      });
  
    }
  
    static obtainTripRequestHistoryData(context)
    {
      var keys = Provider.of&lt;AppData&gt;(context, listen: false).tripHistoryKeys;
  
      for(String key in keys)
      {
        newRequestRef.child(key).once().then((DataSnapshot snapshot)
        {
          if (snapshot.value != null)
          {
            var history = History.fromSnapshot(snapshot);
            Provider.of&lt;AppData&gt;(context, listen: false).updateTripHistoryData(history);
          }
        });
      }
    }
  
    static String formatTripDate(String date)
    {
      DateTime dateTime = DateTime.parse(date);
      String formattedDate = &quot;${DateFormat.MMMd().format(dateTime)}, ${DateFormat.y().format(dateTime)} - ${DateFormat.jm().format(dateTime)}&quot;;
      return formattedDate;
    }
  </code></pre>