Vueer
Vueer

Reputation: 1502

Adding data to existing list from initState in Flutter (Async)

I try to load specific data from my Sembast database to the view on init and add it to an existing list. Why this code does not work?

The data exists, but could it be that my code will never called? I´ve edited the code with print. print("3") does not fire.

bool _checkConfiguration() => true;

  @override
  void initState() {
    _currentDate = widget._currentDate;
    _markedDateMap = widget._markedDateMap;

    if (_checkConfiguration()) {
      print("1"); // FIRES
      Future.delayed(Duration(seconds: 2), () {
        final calendarEntriesDataInitial =
            Provider.of<CalendarEntries>(context, listen: false);

        print("2"); // FIRES
        FutureBuilder<List<CalendarEntry>>(
            future: calendarEntriesDataInitial.getAll(),
            builder: (BuildContext context,
                AsyncSnapshot<List<CalendarEntry>> snapshot) {
              print("3"); // NOT FIRES. HERE´S A PROBLEM!
              if (snapshot.hasData &&
                  snapshot.connectionState != ConnectionState.waiting) {
                for (final entry in snapshot.data) {
                  setState(() {
                    _markedDateMap.add(
                        new DateTime(entry.dateTime),
                        new Event(
                          date: new DateTime(entry.dateTime),
                          title: 'Event 5',
                          icon: _eventIcon,
                        ));
                  });
                }
              }
            });
      });
    }

    super.initState();
  }

Upvotes: 0

Views: 395

Answers (2)

Vueer
Vueer

Reputation: 1502

I found a solution: The FutureBuilder is a widget and can only by used in the widget three. Therefore I use another way to get my required values:

@override
  void initState() {
    _currentDate = widget._currentDate;
    _markedDateMap = widget._markedDateMap;

    Future.delayed(Duration.zero, () {
      final calendarEntriesDataInitial =
          Provider.of<CalendarEntries>(context, listen: false);

      calendarEntriesDataInitial.getAll().then((value) {
        if (value == null || value.isEmpty) {
          return;
        }

        for (final entry in value) {
          if (entry.dateTime != null) {
            final date = DateTime.parse(entry.dateTime);
            setState(() {
              _markedDateMap.add(
                  new DateTime(date.year, date.month, date.day),
                  new Event(
                    date: new DateTime(date.year, date.month, date.day),
                    title: entry.servicePartner,
                    icon: _eventIcon,
                  ));
            });
          }
        }
      });
    });

    super.initState();
  }

Upvotes: 0

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

You have to use setState() for showing updated UI in map

setState(() {
    _markedDateMap.add(...)
    });

Upvotes: 1

Related Questions