0

I'm having touble displaying the results of a Set<String>. I seem to get the results from Future but they don't print on screen. Here is my code:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SavedForms extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _SavedForms();
}

class _SavedForms extends State<SavedForms>{
  List<Card> myForms = [];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          ...myForms,
        ],
      ),
    );
  }

  getKeys() {
    getData().then((result) {
      result.forEach((element) {
        print("displaying ${element.toString()}");
        setState(() {
          myForms.add(new Card(
            child: Text(element.toString()),
          ));
        });
      });
    });
  }

  Future<Set<String>> getData() async {
    final prefs = await SharedPreferences.getInstance();
    final value = prefs.getKeys();
    print("got ${value.length} results");
    return value;
  }
}

The prints result show data being fetched correctly (testElement should be diplayed in screen inside a Card):

I/flutter ( 3411): got 1 results
I/flutter ( 3411): displaying testElement
afarre
  • 512
  • 6
  • 18
  • show us your `myForms` code in Widget build. – John Joe Dec 09 '20 at 11:50
  • `myForms` is the list where the cards are stored: `List myForms = [];` – afarre Dec 09 '20 at 11:55
  • I did try to use a `FutureBuilder`, but didn't get it to work, that's why I tried with the `.then` operator. I'll try again later as the example you provided is quite easier to understand than others, but nonetheless, should work with `.then`. – afarre Dec 09 '20 at 13:43
  • its working at my end, where you are runnning android or ios – Jitesh Mohite Dec 09 '20 at 16:03
  • I checked on emulator and simulator both are working, just restart the screen. – Jitesh Mohite Dec 09 '20 at 16:27
  • Oh shit, the code is correct. The top bar of my device (with the wifi, battery and stuff) was overlapping a tiny card I did not see. My bad. – afarre Dec 10 '20 at 11:46

0 Answers0