0

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();
  }

2 Answers 2

1

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

setState(() {
    _markedDateMap.add(...)
    });
Sign up to request clarification or add additional context in comments.

5 Comments

Not working. I also put an print("Test") into the FutureBuilder, but nothing in the console...
@asored: That means yout FutureBuilder itself not working. Right?
Yes, seems to be like that.
use StreamBuilder instead of FutureBuilder
I have to use FutureBuilder as type. My database requires that :( And in other places its working well, for example in the Widget Tree.... I´ll update my code with print examples. Just a moment..
0

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();
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.