0

Is it possible to refresh appBar title after widget FutureBuilder ? I'd like to set title after FutureBuilder is done

class _SimpleBarChart extends State<SimpleBarChartPage> { String _appBarTitle = ''; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(_appBarTitle)), body: Center(child: futureCAGraph())); } futureCAGraph() { return FutureBuilder( future: BddAMKGraphCA().getDataGraphCA(_caAnnee), builder: (context, AsyncSnapshot<List<dynamic>> snapshot) { if (snapshot.hasData) { return ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (context, int currentIndex) { return affGraphCA(context); }); } else if (snapshot.hasError) { return Text('${snapshot.error}'); } //return a circular progress indicator. return new CircularProgressIndicator(); }); } 

4 Answers 4

1

You Just Have to update Text(_appBarTitle) with Text(snapshot.data[Index].title)

Sign up to request clarification or add additional context in comments.

Comments

0

you can easily update it with setstate() after you get data from future. just assign _appBarTitle in setstate() as shown below,

 setState(() { _appBarTitle=//assign your snapshot data }); 

Comments

0

It's not a good practice to call setState during the build method. Instead you can move Scaffold inside the builder like this:

 Widget build(BuildContext context) { return FutureBuilder( future: BddAMKGraphCA().getDataGraphCA(_caAnnee), builder: (context, AsyncSnapshot<List<dynamic>> snapshot) => Scaffold( appBar: AppBar(title: Text(_appBarTitle)), body: Center(child: (snapshot.hasData) ? ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (context, int currentIndex) { return affGraphCA(context); }) : (snapshot.hasError) ? Text('${snapshot.error}') : //return a circular progress indicator. CircularProgressIndicator(), ), ) ); } 

Comments

0

Sorry, after testing appBar's title don't print anything

Widget build(BuildContext context) { return FutureBuilder( future: BddAMKGraphCA().getDataGraphCA(_caAnnee), builder: (context, AsyncSnapshot<List<dynamic>> snapshot) => Scaffold( appBar: AppBar(title: Text(_appBarTitle)), body: Center(child: (snapshot.hasData) ? ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (context, int currentIndex) { **print('appBar something');** return affGraphCA(context); }) : (snapshot.hasError) ? Text('${snapshot.error}') : //return a circular progress indicator. CircularProgressIndicator(), ), ) ); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.