Saat memuat data dari json saya mendapat beberapa masalah. Saya pikir masalahnya adalah dalam mengambil data. Tahu apa yang terjadi? Saya bukan pengembang Flutter terbaik, ini lebih seperti hobi jadi cobalah untuk bersikap santai. Di sini ...

0
Sebastian Marciniak 17 Juni 2021, 01:20

1 menjawab

Berkat jawaban Stewie, saya menambahkan beberapa kode untuk menangani penyegaran setelah dimuat:

class _RulesCardState extends State<RulesCard> with TickerProviderStateMixin{

  CampusRules rulesListPL;
  CampusRules rulesListEn;
  AnimationController controller;

  @override
  initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async
    {
      rulesListPL = await this.loadFromJson();
      print(rulesListPL);
    });

    controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 5),
    )..addListener(() {
      setState(() {});
    });
    controller.repeat(reverse: true);

    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  Future<CampusRules> loadFromJson() async {

    Map<String, dynamic> campusRulesMap = jsonDecode(await CampusRules.getJsonSting());
    final campusRules = CampusRules.fromJson(campusRulesMap);

    return campusRules;
  }

  @override
  Widget build(BuildContext context) {
    // If rulesListPL has not initialized yet
    if (rulesListPL == null) {
      // Display your loading screen instead
      return Center(
        child: CircularProgressIndicator(
          value: controller.value,
        ),
      );
    }

    return Drawer(
      child: ListView(
        padding: EdgeInsets.zero,
        children: [
          Container(
            height: 90.0,
            child: DrawerHeader(
              child: Text(
                  LocaleKeys.campus_rules.tr(),
                  textAlign: TextAlign.center,
                style: Theme.of(context).textTheme.headline3
              ),
              decoration: BoxDecoration(
                  color: Theme.of(context).bannerTheme.backgroundColor
              ),
            ),
          ),
          for (CampusRule campusRule in rulesListPL.campusRulesList)
            SingleChildScrollView(
              physics: BouncingScrollPhysics(),
              child: buildListTitle(context, campusRule),
            ),
        ],
      ),
    );
  }
0
Sebastian Marciniak 16 Juni 2021, 23:36