I've made the JSON data and appear it into FutureBuilder with ListView.builder widget. I want to create a favorite Icon in the trailing of the ListView.builder. So i created it with IconButton, but when I create setState to make some item as favorited, the data didn't updated.
Here is my code
import 'package:flutter/material.dart'; import 'package:json_test/class/doa.dart'; import 'package:json_test/page/DoaPage.dart'; class MainPage extends StatefulWidget { @override _MainPageState createState() => _MainPageState(); } class _MainPageState extends State<MainPage> { Future<List<Doa>> fetchDoa(BuildContext context) async { final jsonstring = await DefaultAssetBundle.of(context).loadString('assets/doa.json'); return doaFromJson(jsonstring); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("JSON Data test"), ), body: Container( child: FutureBuilder( future: fetchDoa(context), builder: (context, snapshot) { if (snapshot.hasData) { return ListView.builder( itemCount: snapshot.data.length, itemBuilder: (BuildContext context, int index) { Doa doa = snapshot.data[index]; return Card( margin: EdgeInsets.all(8), child: ListTile( title: Text(doa.judul), onTap: () { Navigator.of(context).push(MaterialPageRoute( builder: (BuildContext context) => DoaPage( doa: doa, ))); }, trailing: IconButton( icon: Icon( doa.fav ? Icons.favorite : Icons.favorite_border, color: doa.fav ? Colors.red : null, ), onPressed: () => setState(() => doa.fav = !doa.fav), ))); }, ); } return CircularProgressIndicator(); }))); } }
and this is the preview
https://stackoverflow.com/questions/67040738/why-my-flutter-json-data-didnt-updated-from-setstate April 11, 2021 at 09:38AM
没有评论:
发表评论