1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Check Variable for Updates

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 22:22.

  1. Stack

    Stack Membro Participativo

    I am working on a widget that holds a list view of future appointments. But the information I need is fetched with an async function. This function loads a couple of items a second but takes a while to finish. To speed up the time, I fetched the info the moment the app started.

    Main Method

    Future main() async {
    void initAppointmentList() async {
    await AppointmentList().init(30);
    }
    initAppointmentList();
    }


    Appointment List Fetching

    class AppointmentList {
    List<Map<String, String>> appointments = [];

    Future init(int range) async {
    for (int i = 0; i < range; i++) {
    /*
    HTTP GET Code fetching data
    */
    appointments.add(/* The Data */);
    }
    day = day.add(const Duration(days:1));
    }

    }
    }


    Flutter Widgets

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

    @override
    State<AppointmentOverviewContainer> createState() => _AppointmentOverviewContainerState();
    }

    class _AppointmentOverviewContainerState extends State<AppointmentOverviewContainer> {
    List<Map<String, String>> appointments = AppointmentList().appointments;

    @override
    void initState() {
    super.initState();
    }

    @override
    Widget build(BuildContext context) {
    return (/* If Appointment List Has At Least One Item Loaded */) ?
    FittedBox (
    fit: BoxFit.scaleDown,
    child: Text(appointments[0]['Name'] ?? '',
    style: GoogleFonts.lato(fontSize: 20, fontWeight: FontWeight.w500)),
    ) :
    FittedBox (
    fit: BoxFit.scaleDown,
    child: Text('Loading Appointments',
    style: GoogleFonts.lato(fontSize: 20, fontWeight: FontWeight.w500)),
    ),
    );
    }
    }


    Is there a way that the Flutter widget can check if the appointment list has been updated? If it has, display the most recent one. However, I also want it so it is able to check when the fetching has completely finished.


    • Since the async function was initialized in the main method, I can't use the FutureBuilder


    • I can't use setState because the variable updating is done externally to the widget


    • Putting the fetching inside of the widget prevents me from running it in the beginning

    Continue reading...

Compartilhe esta Página